Skip to content

Instantly share code, notes, and snippets.

@neekey
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neekey/6ee063f9a55bdb3acbf5 to your computer and use it in GitHub Desktop.
Save neekey/6ee063f9a55bdb3acbf5 to your computer and use it in GitHub Desktop.
在chrome插件中实现复制文本内容
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<textarea class="J_ForCopy"></textarea>
<script type="text/javascript" src="./lib/jquery.js"></script>
<script type="text/javascript" src="./background.js"></script>
</body>
</html>
/**
* 选中节点,执行命令。注意,节点必须具备id或者class
*/
function copy(text) {
var copyTextarea = $( '.J_ForCopy' );
copyTextarea.val( text );
copyTextarea.select();
document.execCommand( 'copy', true );
}
chrome.runtime.onMessage.addListener( function( request, sender, sendResponse) {
if (request.type == "copy"){
copy( request.data );
sendResponse( true );
}
});
/**
* 绑定点击事件
*/
taskIdDOM.on( 'click', function(){
copy( taskId );
});
/**
* 向background发送点击事件
* @param text
*/
function copy( text ) {
chrome.runtime.sendMessage({ type: 'copy', data: text }, function(response) {
if( response ){
alert( '复制任务编号 ' + taskId + ' 成功' );
}
});
}
{
"manifest_version": 2,
"name": "pan-helper",
"icons": {
"128": "images/logo_128.png",
"48": "images/logo_48.png",
"16": "images/logo_16.png"
},
"description": "判定小助手",
"version": "0.0.1",
"background": {
"page": "./assets/background.html"
},
"content_scripts": [
{
"matches": ["http://pan.taobao.com/jury/detail.htm*"],
"js": [
"assets/lib/jquery.js",
"assets/content.js"
],
"css": [
"assets/lib/font-awesome/css/font-awesome.css",
"assets/content.css"
],
"all_frames": true
}
],
"permissions": [
// 这里剪贴板的权限很重要
"clipboardWrite",
"clipboardRead",
"http://*/*"
],
"web_accessible_resources": [
"assets/*"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment