13

I am using a Content script to manipulate data in the DOM. I have been using document.execCommand('copy'); successfully on a popup page.

I am now searching for a way to make it work on a Content script. I have checked the limitations for content scripts here, but I do not understand if Clipboard control is limited or not. I have also checked answers here - in stackoverflow, but it seems that most are uncertain and some are from a few years ago so there might have been changes.

Even if it is limited, is it possible to have some kind of workaround?

Thank you!

I am posting the current script that I have.

manifest.json

{
  "name": "Page action by URL",
  "version": "1.0",
  "description": "Прибавка за обработка на данните от НБДН.",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "page_action" :
  {
    "default_icon" : "icon-19.png",
    "default_title" : "Приложение за НБД за PHP"
  },
  "permissions" : [
    "clipboardWrite",
    "clipboardRead",
    "declarativeContent",
    "activeTab",
    "tabs",
    "https://nbd.grao.government.bg/graoappshort/*"
  ],
  "icons" : {
    "48" : "icon-48.png",
    "128" : "icon-128.png"
  },
  "manifest_version": 2
}

background.js

chrome.runtime.onInstalled.addListener(function() {
  // Replace all rules ...
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    // With a new rule ...
    chrome.declarativeContent.onPageChanged.addRules([
      {
        conditions: [
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: { urlContains: 'nbd.grao.government.bg/graoappshort/' },
          })
        ],
        actions: [ new chrome.declarativeContent.ShowPageAction() ]
      }
    ]);
  });
});


chrome.pageAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null, {file: 'page-editor.js'});
  chrome.tabs.insertCSS(null, {file: "style-inject.css"});
});

and the function inside page-editor.js

function(){
      var copyFrom = document.createElement("textarea");
      copyFrom.textContent = PoleIME.value;
      document.body.appendChild(copyFrom);
      copyFrom.focus();
      document.execCommand('SelectAll');
      document.execCommand('Copy');
      //document.body.removeChild(copyFrom);
      }
Yavor Tashev
  • 144
  • 1
  • 1
  • 8
  • Can you confirm that the script is injected at all? You should try passing `tab.id` instead of `null` too. – Xan Sep 02 '14 at 11:34
  • What is `PoleIME`? Is it contained within your `page-editor.js` or is it in the page's own script? – Xan Sep 02 '14 at 11:37
  • Yes, I confirm that the script is injected and the DOM is being manipulated successfully. PoleIME is an input field that is filled with data. It is contained it the script. The textarea copyForm is successfully appended to the body and it is successfully filled and selected. I see the result on the page, but the copying function does not work. The whole script is in a working condition and not optimized and is 6600 rows - that is why i did not include it. – Yavor Tashev Sep 02 '14 at 12:06
  • `var PoleIME=document.createElement("input"); PoleIME.type="text"; PoleIME.name="IME"; PoleIME.id="IME"; PoleIME.size=60; PoleIME.value="whatever text i put inside";` – Yavor Tashev Sep 02 '14 at 12:09

1 Answers1

26

Content scripts cannot use the clipboard at the moment. In the future, once crbug.com/395376 is resolved, then the code as shown in the question will work as intended.

Until that bug is fixed, you have to send the data to the background page and copy the text from there:

// content script
chrome.runtime.sendMessage({
    type: 'copy',
    text: 'some text to copy'
});

Script on background page or event page:

chrome.runtime.onMessage.addListener(function(message) {
    if (message && message.type == 'copy') {
        var input = document.createElement('textarea');
        document.body.appendChild(input);
        input.value = message.text;
        input.focus();
        input.select();
        document.execCommand('Copy');
        input.remove();
    }
});
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Thanks a lot for your help. Hope I get this thing to work tomorrow at the office. Will comment then. – Yavor Tashev Sep 02 '14 at 16:07
  • 2
    The bug is status fixed since 9/2014 – Günter Zöchbauer Feb 25 '15 at 19:10
  • I can't get this work at on my mac [chrome Version 49.0.2623.87 (64-bit)] – Brook Mar 19 '16 at 03:57
  • @Brook It works here (Linux), so it's probably a Mac-specific bug. There are quite some bugs on the issue tracked, e.g. https://crbug.com/467296. Your bug sounds like https://crbug.com/334062, "apps: writing to the clipboard does not work from a background page, but works from an app window page". Someone [suggested](https://crbug.com/334062#c5) to use a non-zero width to the `` or `` element. Give it a try! – Rob W Mar 19 '16 at 18:28