-- Open JavaScript

javascript = {}

function javascript.prefcap(pref, type, expect, func)
	local now = getpref(type, pref)
	if now == expect then
		func()
	else
		setpref(type, pref, expect)
		func()
		setpref(type, pref, now)
	end
end

function javascript.jscap(f)
	if getpref(TYPE_BOOL, "javascript.enabled") then
		f()
	else
		setpref(TYPE_BOOL, "javascript.enabled", true)
		f()
		setpref(TYPE_BOOL, "javascript.enabled", false)
	end
end

function javascript.pbcap(f)
	if getpref(TYPE_BOOL, "dom.disable_open_during_load") then
		setpref(TYPE_BOOL, "dom.disable_open_during_load", false)
		f()
		setpref(TYPE_BOOL, "dom.disable_open_during_load", true)
	else
		f()
	end
end

-- run script with popup
function javascript.popup(script)
	script = string.gsub(script, "%c", "")
	javascript.pbcap(function ()
		javascript.jscap(function ()
			NavigateTo(script, OPEN_NORMAL)
		end)
	end)
end

-- run script without popup
function javascript.run(script)
	script = string.gsub(script, "%c", "")
	javascript.jscap(function ()
		NavigateTo(script, OPEN_NORMAL)
	end)
end
