ffmmx's Blog

Happy coding

CHROME插件 “复制选中的链接地址”

ffmmx posted @ 2011年11月10日 23:13 in javascript with tags chrome plugin javascript , 9141 阅读

    昨日本来在人人影视上面下载美剧,结果那部电视剧太多集了。CHROME下那个复制全部不能用。只好自己写一个。

代码如下:

注册文件 manifest.json

 

{
  "name": "复制选中的链接地址",
  "description": "复制选中的链接地址到剪贴板中(电驴,离线什么的)",
  "version": "0.1",
  "permissions": ["contextMenus","tabs","https://*/*","http://*/*"],
  "background_page": "background.html"
}

实现选中链接文件(之前对CHROME也没有什么了解,临时从chrome API那里下载的一个例子,文件名忘改了) sample.js

 

var selection = window.getSelection();
if(selection.rangeCount > 0)
{
	var range = selection.getRangeAt(0);
	
	var div = document.createElement('div');
	//把复制的内容放入div中,转化为DOM结构
	div.appendChild(range.cloneContents());

	var contents = "";
	//获取div里面的,链接元素
	var aucher_list = div.getElementsByTagName("a");
	
	for (var i = 0; i < aucher_list.length; i++) {
		contents += "\n" + aucher_list[i].href;
	}

	contents = contents.substring(1);
	//向background发送ED2K链接
	chrome.extension.sendRequest({text:contents});
}

background.js

 

<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
	
		<title>Background</title>
		<script type="text/javascript" charset="utf-8">
			chrome.extension.onRequest.addListener(function (msg, sender, sendResponse) {

					var textarea = document.getElementById("lnktext");
					
					textarea.value = msg.text;					
					textarea.select();
					document.execCommand("copy", false, null);

					sendResponse({});
					}
					);	


			function copylinkbyselected(info,tab){
				
				chrome.tabs.executeScript(null, { "file" : "sample.js", "allFrames" : true } );
			}

chrome.contextMenus.create(
		{
		"title": "copy link by selected",
		"contexts":["selection"],
		"type":"normal",
		"onclick": copylinkbyselected
		});	
		</script>
	</head>
	<body>
		<textarea id="lnktext" rows="8" cols="40"></textarea>
	</body>
</html>
Avatar_small
iXie5 说:
2012年2月28日 15:34

请问一下,为什么我弄了却不能用,希望把这个扩展打包发到我邮箱.谢谢
我的邮箱 policeman@vip.qq.com

Avatar_small
ffmmx 说:
2012年7月17日 11:24

现在才看到。一会儿把 这个发到你邮箱里

Avatar_small
KVS Model Paper 说:
2022年9月29日 20:42

KVS Sample Paper 2023 Pdf Download for Kendriya Vidyalaya Sangathan Class 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 & 12 Arts, KVS Model Paper Science & Commerce Stream Practice Paper Suggestions with Past years old exam Solved Question Bank for all Regional Students of English Medium, Hindi Medium & Urdu Medium Studying in KVS Schools across the Country. All the Kendriya Vidyalaya Sangathan Board Students can download the Sample Paper Suggestions with Model Papers along with Previous Years old Exam Solved Question Bank for all Languages & Subjects of the Course.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter