#ß K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage2)
#
# ---------------------------------- LinkToggle v3 ----------------------------
#  Toggle a link into normal text to easier select parts of the link name, then restore the link again.
#  v3: Additionally for KM1.6 + KM75 or newer:   Copy link NAME
#
#  Version :   		 v3 / 2015-03-22 (by siria)
#  Forum:  		http://kmeleon.sourceforge.net/forum/read.php?9,115867
#  K-Meleon version:   KM 1.5.4 or newer.  
#  Preferences :   	Can toggle the native KM pref "accessibility.browsewithcaret"
#
#  MENU :    "LinkToggle" in context menu (right-click) of links.  Plus KM1.6/KM75:  "Copy Link Name"
#  + Menu after first use: "LinkToggle Restore". Placed below "Reload" menu, if you right-click near the link
#  + some automatic ways for restoring toggled links:
#  If you COPY something immediately with Ctrl+C in same page (frame), toggled links are automatically restored.
#  Also if you SELECT something, right-click on it, and choose one of the "Restore +COPY/+SEARCH/+GO" options.
#  Those extra menu lines exist only shortly after a link toggle, then vanish again.
#  Note that on FRAMES pages, restoring works in active frame only.
#  But of course reloading a page always restores everything too. 

# ----------------------  Alternative:   Text Cursor (Caret) ----------------------
#  On a few websites "LinkToggle" doesn't work. In such cases right-click on the link and use "Text Cursor 
#  (select in links)" This toggles one of the hundred hidden prefs in Mozillas gecko engine: "browse with caret". 
#  Now if you click inside a page, that produces a visible cursor which you can move with the arrow keys 
#  even inside a link, then click Shift+Arrow to select some text. 
#  Catch is: This special pref also changes the Home + End keys, and PageUp+Down keys, that's why 
#  this macro is coded to RESET it automatically as soon as possible: if you use Ctrl+C or at next page load.

#------------------------------   USER  CONFIG  --------------------------

_LiTo_menu_UserConfig{

#  --->  Optional menu lines, choose if or which you want or not?
#  To hide a menu entry, just add a # comment sign at beginning of "setmenu"

#  If a link is coded to open a new page, you can right-click and force it to open in the current tab:
setmenu("LinkOpen",command,"Open Here",ID_OPEN_LINK,2);

#  If you select a link and right-click it, there's by default only "Copy Link URL". 
#  Trick: If you move the mouse outside, you get the menu to "Copy" the selected text.
#  Add this usual command "Copy" also to the link menu:
setmenu("LinkCopy",command,"&Copy",ID_EDIT_COPY,0);

#  KM75 or newer get "Copy Link Name" into clipboard.  If additionally you want it in the URLBAR:
###  setmenu("LinkCopy",macro,"Copy Link Name > URL bar",LinkToggle_copyNameUbar,"&Copy Link URL");

#  "Text Cursor" is an alternative, handy way to select parts of a link text. 
#  If toggled ON with this macro, it is soon toggled back automatically. Explanation see further down.
setmenu("LinkCopy",macro,"Text Cursor (select in links)",LinkToggle_pageCursor,-1);
setmenu("PageDisplay",macro,"Text Cursor (select in links)",LinkToggle_pageCursor,-1);

}
#==================   END  OF USER  CONFIG  ==================

LinkToggle_copyName{
macroinfo=_("Copy link name");
$JS_doitLink="linktext=text ;"; &JS_hndlLink;
# "return" only works in KM1.6 and KM75 (and hopefully newer):
$_x=injectJS("(function(){return linktext;})() ");
$_x>"" ? setclipboard($_x) : 0;
}

LinkToggle_copyNameUbar{
macroinfo=_("Copy link name into URL bar and clipboard");
&LinkToggle_copyName;
$_x>"" ? $URLBAR=$_x : 0;
$_x="";
}

LinkToggle_doit{
macroinfo=_("Toggle a link inactive to allow text selection. To restore it, right-click on it or copy anything.");
$JS_doitLink="var hrefbak=getAttribute('href'); if (hrefbak) {setAttribute('hrefbak',hrefbak); removeAttribute('href');}";
&JS_hndlLink;
#
#  At first use in a session, add "Restore" command below "Reload" menu.
setmenu("Nav",macro,"LinkToggle Restore",LinkToggle_restore,-1);
setmenu("Reloa&ding",macro,"LinkToggle Restore",LinkToggle_restore,-1);
#
#  TEMPORARILY add 4 restore commands to "Selection" context menu
#  (removed automatically after next restore, Ctrl+C, or page load)
setmenu($_LiTo_menuSelect,macro,"LinkToggle Restore",LinkToggle_restore,1);
setmenu($_LiTo_menuSelect,macro,"    \"    "._("+ Copy"),LinkToggle_copyrestore,1);
setmenu($_LiTo_menuSelect,macro,"    \"    "._("+ Search"),LinkToggle_ReSearch,2);
setmenu($_LiTo_menuSelect,macro,"    \"    "._("+ Go To"),LinkToggle_ReGo,3);
#
# TEMPORARILY Change Ctrl+C to automatically restore toggled links before copying a selected text. 
#  Ctrl+C  will be reset again after first use, or at next restore, or next page load
setaccel("CTRL C","macros(LinkToggle_copyrestore)");
#
$OnLoadOnce=$OnLoadOnce."_LinkToggle_resetMenu;";
}

_LinkToggle_resetMenu{
#  Reset Ctrl+C to the system's normal copy command again:
setaccel("CTRL C","id(ID_EDIT_COPY");
#  Remove the 4 menu entries in Selection Context menu, to avoid clutter:
setmenu($_LiTo_menuSelect,macro,"",LinkToggle_restore);
setmenu($_LiTo_menuSelect,macro,"",LinkToggle_copyrestore);
setmenu($_LiTo_menuSelect,macro,"",LinkToggle_ReSearch);
setmenu($_LiTo_menuSelect,macro,"",LinkToggle_ReGo);
}

LinkToggle_restore{
macroinfo=_("Restore toggled inactive links in current frame");
$JS_doitLink="var hrefbak=getAttribute('hrefbak'); if (hrefbak) setAttribute('href',hrefbak); ";
&JS_hndlLink;
&_LinkToggle_resetMenu;
}

# ===============  On Temporary Menu ===============

LinkToggle_copyrestore{
macroinfo=_("Restore toggled links and copy selection into clipboard");
menugrayed=!$SelectedText;
id(ID_EDIT_COPY);
&LinkToggle_restore;
}

LinkToggle_ReSearch{
macroinfo=_("Restore toggled links and search for selected text in the web (=clicking Search globe)");
menugrayed=!$SelectedText;
&LinkToggle_restore;
&Search;
}

LinkToggle_ReGo{
macroinfo=_("Restore toggled links and open selected text as URL (=clicking Go button)");
menugrayed=!$SelectedText;
&LinkToggle_restore;
&Go_Selected;
}

# =============== Text Cursor (Caret) ===============

LinkToggle_pageCursor{
macroinfo=_("Temporarily show text cursor in page, to select parts of a link (Toggle pref 'accessibility.browsewithcaret')");
menuchecked=getpref(BOOL,"accessibility.browsewithcaret"); 
$_x=togglepref(BOOL,"accessibility.browsewithcaret"); 
$_x=="false" ? setaccel("CTRL C","id(ID_EDIT_COPY"):0;
$_x=="true" ? setaccel("CTRL C","macros(_LiTo_copyCaretOff)"):0;
$OnLoadOnce=$OnLoadOnce."_LiTo_caretOff;";
}

_LiTo_copyCaretOff{
id(ID_EDIT_COPY);
&_LiTo_caretOff;
}

_LiTo_caretOff{
setpref(BOOL,"accessibility.browsewithcaret","false"); 
#  At first use of the modified shortcut "Ctrl+C", reset it to the systems normal "Copy" command:
setaccel("CTRL C","id(ID_EDIT_COPY");
}

# =============== Menu-1  (Do not change this one) ===============

_LinkToggle_BuildMenu{
setmenu("LinkCopy",macro,"LinkToggle",LinkToggle_doit,-1);
#
#  Plus "Copy Link Name" if KM75 or newer (gecko31):
$_v = iniread("Build","Milestone","", getfolder(RootFolder)."\\platform.ini");
if ($_v>"30") setmenu("LinkCopy",macro,"Copy Link Name",LinkToggle_copyName,"&Copy Link URL");
#
#  Plus "Copy Link Name > URL bar" if KM1.6 (other versions nothing):
$_v=readfile(getfolder(RootFolder)."\\defaults\\pref\\kmeleon.js");
$_v=substr($_v,index($_v,"\"1.")+1,3); 
if ($_v=="1.6") setmenu("LinkCopy",macro,"Copy Link Name > URL bar",LinkToggle_copyNameUbar,"&Copy Link URL");
#
###  (Restore-menu will be built after first use of "LinkToggle" in a session, see above)
###  setmenu("Nav",macro,"LinkToggle Restore",LinkToggle_restore,-1);
#
#  Add empty "Restore" placeholder in context menu of SELECTED text. 
#  That's for the temporary entries which appear after a toggle action, otherwise hidden
$_LiTo_menuSelect="LinkToggle_Select";
setmenu("Select",inline,$_LiTo_menuSelect,-1);
#
#  Read above for customizing optional menu entries
&_LiTo_menu_UserConfig;
}
JS_hndlLink{
$JS_doit="for(var j=0,a=getElementsByTagName('a');j<a.length;j++)with(a[j])if(urlencode(href)==urlencode(\"".urlencode($LinkURL)."\")){".$JS_doitLink."}";
&JS_hndlDoc;
}
#------------------------ at browser start --------------------
$OnInit=$OnInit."_LinkToggle_BuildMenu;";
$macroModules=$macroModules."LinkToggle;"; 