--[[  ViewLinks

luamacro(ViewLinks, Page Links)
luamacro(ListLinks, List Links)
]]
require "javascript"

function ViewLinks()
	javascript.popup([[javascript:
	LnkW=open('','LnkW','width=600,height=300,scrollbars,resizable');
	sLFw=window.name;
	if(!sLFw){
		sLFw=window.name='sLFw'
	};
	sLFd=document;
	DlnK=document.links;
	with(LnkW.document){
		write('<base target='+sLFw+'>'+DlnK.length+' total links from:<br><b>'
			+sLFd.title+'</b><br>'+sLFd.URL.link(sLFd.URL)+'<hr>');
		for(lNK=0;lNK<DlnK.length;lNK++){
			write('<p>'+DlnK[lNK].text+'<br>'+DlnK[lNK].href.link(DlnK[lNK]))
		};
		close();
		LnkW.focus()
	}]])
end


function ListLinks()
	javascript.popup([[javascript:
	LnkW=open('','LnkW','width=600,height=300,scrollbars,resizable');
	sLFw=window.name;
	if(!sLFw){
		sLFw=window.name='sLFw'
	};
	sLFd=document;
	DlnK=document.links;
	LstL={};
	with(LnkW.document){
		write('<base target='+sLFw+'>'+DlnK.length+' total links from:<br><b>'
			+sLFd.title+'</b><br>'+sLFd.URL.link(sLFd.URL)+'<hr>');
		for(lNK=0;lNK<DlnK.length;lNK++){
			if (! LstL[DlnK[lNK].href]) {
				write(DlnK[lNK].href.link(DlnK[lNK])+'<br>');
				LstL[DlnK[lNK].href] = 1;
			}
		};
		close();
		LnkW.focus()
	}]])
end

function ViewSelection()
	local pref=getpref(TYPE_BOOL,"browser.popups.showPopupBlocker")
	setpref(TYPE_BOOL,"browser.popups.showPopupBlocker",false);
	javascript.popup([[javascript:
	html_selection=document.createElement('div');
	if (window.getSelection().rangeCount>0){html_selection.appendChild(window.getSelection().getRangeAt(0).cloneContents())};
	scripts = html_selection.getElementsByTagName('SCRIPT');
	for(i=scripts.length-1; i>=0; i--){scripts[i].parentNode.removeChild(scripts[i])};
	s_old=html_selection.innerHTML.replace(/<\/(TEXTAREA>)/ig, '&lt;/$1');
	if (html_selection.firstChild.nodeName=='TH' || html_selection.firstChild.nodeName=='TR' || 
	    html_selection.firstChild.nodeName=='TD' || html_selection.firstChild.nodeName=='TBODY'){
	  r=document.createRange();r.setStartBefore(html_selection.firstChild);r.setEndAfter(html_selection.lastChild);
	  t=document.createElement('table');r.surroundContents(t);
	  r.detach();
	  html_selection.firstChild.border='1';
	  html_selection.firstChild.width='100%';
	}
        s_new=html_selection.innerHTML.replace(/<TEXTAREA id="V_S_H"/ig, '<TEXTAREA');
	h ='<SCRIPT LANGUAGE="JavaScript">onload=function(){t=document.getElementById("V_S_H");t.select();t.focus()};</SCRIPT>'
	  +'<TABLE style="width:90%;"><TR><TD><I><B>Selection:</B></I></TD></TR><TR><TD style="border:2px inset blue;">'+s_new
	  +'</TD></TR><TR><TD></TD></TR><TR><TD><I><B>HTML:</B></I></TD></TR><TR><TD><TEXTAREA id="V_S_H" ROWS="20" wrap=soft style="width:100%;">'+s_old
	  +'</TEXTAREA></TD></TR><TR><TD><I><B>End</B></I></TD></TR></TABLE>';
	W=open('','','scrollbars,resizable');
	with(W){
		document.write(h);
		document.close();
		focus()
	}]])
	setpref(TYPE_BOOL,"browser.popups.showPopupBlocker",pref);
end
