JavaScript Ninjawords Bookmarklet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript Ninjawords Bookmarklet相关的知识,希望对你有一定的参考价值。

javascript:(function(){function t(f){try{if(f.getSelection)return f.getSelection().toString();if(f.document.selection)return f.document.selection.createRange().text}catch(e){}return ''}function getQ(f){var q=t(f).replace(/^[^a-z0-9-]+|[^a-z0-9-]+$/ig,'').replace(/[^a-z0-9-]+/ig, ',');var s='';for(var i=0;i<f.length;i++){s=getQ(f[i]);if(s)q+=(q?',':'')+s}return q}if(window.location.hostname!='ninjawords.com')window.open('http://ninjawords.com/'+getQ(window.top));else{document.getElementById('q').value = getQ(window.top);Ninjawords.textboxQuery()}})()

//Here it is expanded so you can read it:
javascript:(function(){
	function t(f)	//get the selected text in frame f
	{
		try{
			if(f.getSelection) return f.getSelection().toString();
			if(f.document.selection) return f.document.selection.createRange().text;
		}catch(e){}	//if the frame is in another domain
		return '';
	}
	function getQ(f)	//get the selected text in frame f and its children
	{
		//get rid of non-alphanumeric characters and separate words with commas
		var q = t(f).replace(/^[^a-z0-9-]+|[^a-z0-9-]+$/ig,'').replace(/[^a-z0-9-]+/ig, ',');
		var s = '';
		for(var i=0; i<f.length; i++)	//for each child frame
		{
			s = getQ(f[i]);
			if(s) q += (q?',':'')+s;
		}
		return q;
	}
	if(window.location.hostname != 'ninjawords.com')	//if not already at ninjawords.com
		window.open('http://ninjawords.com/'+getQ(window.top));
	else
	{
		document.getElementById('q').value = getQ(window.top);
		Ninjawords.textboxQuery();
	}
})()

以上是关于JavaScript Ninjawords Bookmarklet的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript函数声明提升

Javascript小括号“()”的多义性

Javascript:带有公共访问器的私有对象

JavaScript入门

判断常用语句结构中的真假

While 循环使用 Await Async。