# K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)

# ---------- Continuous Reload -------------------------------------------------------------------------------------
#
# Dependencies	: main.kmm ($macroPrefBranch, $pref_Frames, ExecMacros, OpenURL)
# Resources	: -
# Preferences	: kmeleon.plugins.macros.reload.interval
#
# ------------------------------------------------------------------------------------------------------------------

Reload_Continuously{
macroinfo=_("Continuously reload the current page");
menugrayed=(index($URL,"about:")==0)+(index($URL,"chrome:")==0)+(index($URL,"wyciwyg:")==0);
$__noAvail=(index($URL,"about:")==0)+(index($URL,"chrome:")==0)+(index($URL,"wyciwyg:")==0);
$ExecMacros="_Reload_1st"; $__noAvail?0:&ExecMacros_JavaScript;
}

# ----- PRIVATE

$_Reload_Interval=$macroPrefBranch."reload.interval";
$_Reload_OnLoad="";

_Reload_1st{
# When frames are toggled from disabled to enabled, the current page must be loaded into a new window/layer/tab
$_Reload_OnLoad="_Reload_2nd";
$OpenURL=$URL; $ExecMacros=getpref(BOOL,$pref_Frames)?$_Reload_OnLoad:"OpenURL_InNew"; &ExecMacros_Frames;
}
_Reload_2nd{
$_Reload_OnLoad="";
$__pre=getpref(INT,$_Reload_Interval);
$__sec=prompt(_("Enter number of seconds between reloads:"),_("Continuous Reload"),$__pre);
$__sec==""?0:injectJS("(function(){var t=parseInt('".$__sec."');if(!isNaN(t)&&(t>0))with(document){open();write('<html><head><style type=\"text/css\">#b{position:absolute;left:0;width:100%;height:2em;bottom:0;padding:0 0 0.25em;font:inherit;}#i{position:absolute;left:0;width:100%;bottom:2em;height:100%;border:0;}</style></head><body style=\"font:menu\"><iframe id=\"i\" src=\"'+location.href+'\"></iframe><button id=\"b\" onclick=\"quit()\"></button></body><script type=\"text/javascript\">var tspan='+t+',tleft=tspan+1,buttn=document.getElementsByTagName(\"button\")[0],units=new Array(\""._("hours")."\",\""._("minutes")."\",\""._("seconds")."\"),title=\""._("Reloading: %s")."\",btext=\""._("Reloading in %t %u (Click to abort)")."\";doit();setInterval(\"doit()\",1000);function doit(){if(!--tleft){tleft=tspan;frames[0].location.reload()}var h=Math.floor(tleft/3600),m=Math.floor((tleft%3600)/60),s=tleft%60,ttext=((h)?(h+((m<10)?\":0\":\":\")+m):((m)?m:\"\"))+((h||m)?((s<10)?\":0\":\":\"):\"\")+s,utext=((h)?units[0]:((m)?units[1]:units[2]));buttn.innerHTML=btext.replace(/%t/,ttext).replace(/%u/,utext);document.title=title.replace(/%s/,frames[0].document.title)}function quit(){history.back()}</script></html>');close()}})()");
setpref(INT,$_Reload_Interval,$__sec); getpref(INT,$_Reload_Interval)>0?0:setpref(INT,$_Reload_Interval,$__pre);
}

_Reload_BuildMenu{
$__r="Reloa&ding";
$kTabs?0:setmenu(Nav,popup,$__r,ID_NAV_FORCE_RELOAD);
$kTabs?0:setmenu(Nav,command,"",ID_NAV_FORCE_RELOAD);
$kTabs?0:setmenu($__r,command,"&Force Reload",ID_NAV_FORCE_RELOAD);
setmenu($__r,macro,"&Continuous Reload...",Reload_Continuously);
}
$OnInit=$OnInit."_Reload_BuildMenu;";

_Reload_OnLoad{
macros($_Reload_OnLoad);
}

_Reload_SetDefaults{
getpref(INT,$_Reload_Interval)==0?setpref(INT,$_Reload_Interval,120):0;
$OnLoad=$OnLoad."_Reload_OnLoad;";
}
$OnStartup=$OnStartup."_Reload_SetDefaults;";

# ------------------------------------------------------------------------------------------------------------------
$macroModules=$macroModules."Reload;";