I have a content script that is declared as
"content_scripts": [
{
“matches”: ["<all_urls>"],
“js”: [“js/content.js”]
}
],
My background scripts are declared as
{
“background”: {
“scripts”: [ “js/storage.js”, “js/background.js”]
},
My permissions are
"permissions": [ “storage”, “contextMenus”, “http:///”, “https:///”,
“activeTab”, “tabs”,“clipboardRead”, “clipboardWrite” ],
To send a message from background.js I use
function sendCopyToContentScript( toBeCopied){
chrome.tabs.query({active: true, currentWindow: true},
function(tabs) {
console.log("sending " + toBeCopied);
chrome.tabs.sendMessage( tabs[0].id, {data: toBeCopied, dir:‘inClpbrd’},
function(){} );
});
}
In content.js, I use this to react to the message sent
chrome.runtime.onMessage.addListener(
function (request, sender){
if (request.cmd) {
…
}
else if (request.data) {
console.log("received from background " + request.data + " " +
request.dir);
if (request.dir == “fromClpbrd”) {
insertTextAtCursor(request.data);
}
else if (request.dir == “inClpbrd”){
if(document.getElementById(‘filteredClipboard_data’) == null){
var ta = document.createElement(‘textarea’);
ta.contentEditable = “true”; ta.id = ‘filteredClipboard_data’;
document.body.appendChild(ta);
}
ta.value = request.data;
ta.select();
document.execCommand(“Copy”);
}
}
});
However I don’t see the “received from background” message and I get the
error
Unchecked lastError value: Error: Could not establish connection. Receiving
end does not exist.
I did some progress, I got this lines to copy in the clipboard
in the content script
console.log(request.dir);
var ta;
if(document.getElementById(‘filteredClipboard_data’) == null){
ta = document.createElement(‘textarea’);
ta.contentEditable = “true”; ta.id = ‘filteredClipboard_data’;
The problem is that the textarea is realy created at the end of the page. So when the menu is selected the page move down.
the
ta.visible="false";
does nothing
Thanks for any suggestion
F.
Well, if you append your element to the body, it’s obviously going to be pretty much the last element on that page. (Only elements appended to the documentElement will come after it.)
If you want it to be the first, do document.body.insertBefore(ta, document.body.firstChild).
And you can position your element with CSS.
By the way, what are you trying to do with that textarea? Do you want to write stuff to the clopboard?