title: Chrome Selection.addRange() does not select (an execCommand('copy') use case)
body: <p>Writing a tiny browser extension in Chrome to copy some specific text from specific web pages to the clipboard. In HTML format, so people could paste it to office programs like word, outlook etc.</p>
  
  <p><code>document.execCommand('copy')</code> is the command I use, it is being triggered by an <code>document.onkeydown</code> key combination (Alt+1), and it works fine - but only for the first time. If you try to press the key combination again, it will do nothing.</p>
  
  <p>I've found the reason for it, <code>document.queryCommandEnabled("copy")</code> returns true for the first time, and false for any additional attempt. If I reload the page, it returns true again for the first time. Furthermore, if I click outside of the browser window after loading the page, and then click in the browser and use the key combination, is immediately returns false, even for the first time.</p>
  
  <pre><code>function copy(text) {
    var sel = document.createElement("div"); // Creating temporary holder for text to copy
  
    sel.style.opacity = 0;             sel.style.position = "absolute";  // These are not really required,
    sel.style.pointerEvents = "none";  sel.style.zIndex = -1;            // at least for Chrome
  
    sel.innerHTML = text; // Put the text to copy into the temporary holder
  
    document.body.appendChild(sel); // Add the temporary holder to the page
  
    var range = document.createRange();     // All this is required to select the text,
    range.selectNode(sel);                  // since the holder is not an editable text
    window.getSelection().addRange(range);  // field and must be treated differently.
  
    // document.designMode = 'on'; (Tried this, no effect)
  
    /* Debugging */ alert("Enabled = " + document.queryCommandEnabled("copy") + " Design mode = " + document.designMode);
  
    try {
      document.execCommand('copy'); // Copy to clipbopard
    }
    catch (err) {
      alert('Unable to copy');
      console.log('Unable to copy'); // Copy failed?!
    }
  
    // document.designMode = 'off'; (Tried this, no effect)
  
    document.body.removeChild(sel); // Clean up removing the temporary holder
  }
  
  document.onkeydown=function(e){
    if(e.altKey &amp;&amp; e.which == 49) { // Alt+1
      copy(link_variable);
      return false;
    }
  }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Adding the manifest file:</p>
  
  <pre><code>{
    "manifest_version": 2,
    "name": "Usage text",
    "version": "0.2",
    "description": "Whatever",
    "content_scripts": [
      {
        "matches": [
          "*://some.specific.site.com/*"
        ],
        "js": ["content.js"]
      }
    ],
    "background": {
      "scripts": ["background.js"]
    },
    "browser_action": {
        "name": "Whatever",
        "default_icon": "icon.png"
    },
    "permissions": [
      "tabs",
      "clipboardWrite"
    ]
  }
  </code></pre>
  
  <h2>Update:</h2>
  
  <p>Transferred the operation from the <strong>content</strong> script to the <strong>background</strong> script, no change.</p>