javascript - Chrome Extension - How to message DOM from content script to background page? -
all want copying text working in chrome extension. found answer, the proper use of execcommand("paste") in chrome extension, gives copy() function:
function copy(str) { var sandbox = $('#sandbox').val(str).select(); document.execcommand('copy'); sandbox.val(''); } the problem is, doesn't work if put on background page , call because background page doesn't have access dom elements (and can't access $('#sandbox')). how can send dom element $('#sandbox') content.js (which can access dom) script background.js script?
i figured out how send message content.js script background.js script , receive response through this:
content.js:
$('#copybutton').click(function(){ chrome.runtime.sendmessage({callcopyfunc: "callstart"}, function(response) { console.log(response.callcopyfunc); }); }); background.js:
chrome.runtime.onmessage.addlistener(function(request,sender,sendresponse){ if(request.callcopyfunc == "callstart"){ sendresponse({callcopyfunc: "callfinished"}); } }); that works great! when click "copybutton" element on webpage (just button), content.js sends "callstart" background.js , background.js sends "callfinished" displayed in console log.
my question is: how send dom element $('#sandbox') background.js file content.js can use copy() function on background page itself? i'm not understanding how send element, text.
sorry if simple, i've been stuck on 2 days. thanks!
well after hours of spending time on should simple chrome clipboard api call, found solution. don't think needs it, i'll post here anyway since spend way time on looking solution.
i downloaded "plus trello" on chrome app store , looked @ it's code. essentially, copy functionality done on background.html (background page) , background.js (script included on background page).
in manifest need these 2 things:
"background": { "page": "background.html", "persistent": true }, "permissions":["clipboardwrite"], then in background.html page need include background.js script , include div id use in background.js script:
<html> <head> <title></title> <link href="css/style.css" rel="stylesheet" type="text/css"> <script type="text/javascript" src="background.js"></script> </head> <body> <div id="selectionplaceholder"></div> </body> </html> then, in background.js script (which included on background.html), need function (i got "plus trello" extension):
function handlecopyclipboard(html) { if (window.getselection && document.createrange) { var elemreplace = document.getelementbyid("selectionplaceholder"); elemreplace.innerhtml = html; //this copy var sel = window.getselection(); var range = document.createrange(); range.selectnodecontents(elemreplace); sel.removeallranges(); sel.addrange(range); document.execcommand("copy"); elemreplace.innerhtml = ""; //blank when done return; } } and need pass function text. passed text content.js script using runetime.onmessage messaging system described in original post. can called right background.js since script has access background.html page (it's included in it).
edit: , also, if prefer shorter copy() function, can include jquery.js on background.html page, include <textarea id="sandbox></textarea> on background.html page, , call copy() background.js file.
Comments
Post a Comment