0

Can anyone tell me why I cannot get a string for the selected text on any given page using windows.getSelection().toString()?

The script is running in the background, even though I've named it as popup.js.

Everything I can find shows a variation of this, but in my code it always returns null. I am calling window.getSelection() on a shortcut combination being pressed. Text is definitely selected, but the result is always null.

manifest.json
{

  "name": "CM_TextDiff",
  "version": "1.0",
  "manifest_version": 2,


  "description": "Highlight changes in two lines of text. Useful to language teachers to highlight corrections.",
  "icons": {
    "128": "compare128.png"
  },

  "background": {
    "scripts": [
      "popup.js"
    ]
  },

    "browser_action": {
      "default_title": "highlight your corrections",
      "default_popup": "popup.html"
    },

    "permissions": [
      "tabs",
      "activeTab",
      "http://*/*", 
      "https://*/*",
      "clipboardRead",
      "clipboardWrite",
      "contextMenus"      
    ],

popup.js
chrome.commands.onCommand.addListener(function(command) {
    console.log('Command:', command);
    getSelectedText();
});

function getSelectedText(){
    alert ("The text content of the selection:\n" + window.getSelection().toString ());
}

I would expect this to display a popup with the text that is selected on the page. However, the result is always a nullstring.

Alex Russell
  • 189
  • 1
  • 10
  • Possible duplicate of [How to access the webpage DOM rather than the extension page DOM?](https://stackoverflow.com/questions/4532236/how-to-access-the-webpage-dom-rather-than-the-extension-page-dom) – wOxxOm Jun 20 '19 at 15:36
  • no, it's not a duplicate. It's a background process. I just named it popup.js because I started by following tutorials. – Alex Russell Jun 20 '19 at 15:53
  • It is a duplicate and it equally applies to any internal extension script including the background script. – wOxxOm Jun 20 '19 at 15:57
  • So what is the solution then? Because I don't get it from the article you linked to – Alex Russell Jun 20 '19 at 17:52
  • There's an answer in that link that explains you need a content script, lists two ways to run it, and contains an example at the end. – wOxxOm Jun 20 '19 at 18:19
  • Hi wOxxOm. I really don't get how this is a duplicate, and don't know how to apply the example to my code. It seems you are an expert and this is very easy for you. Could you show me a code example with getSelection, please? None of the examples of using getSelection have shown it used with tabs.execueScript, and I have already enabled permissions for tabs. The article isn't closely related enough for me to understand how it applies.Thanks in advance. – Alex Russell Jun 20 '19 at 18:35
  • chrome.tabs.executeScript({code: 'getSelection().toString()'}, res => { alert(res[0]) }); note how you need to use the results inside the callback because the API is asynchronous – wOxxOm Jun 21 '19 at 04:09
  • Thank you, wOxxOm, that works, though I still need to get the results out of the callback, so I can process them. Is that possible? Everything I have tried gives me an undefined variable or throws an error about not being able to access a Chrome URL – Alex Russell Jun 21 '19 at 07:55
  • See a tutorial/article about how asynchronous JavaScript works. In short it's like a one-time event that's triggered in the future so it runs much later after the outside of the callback has already finished. – wOxxOm Jun 21 '19 at 08:13

1 Answers1

0

For anyone else who gets stuck on this:

Be sure to have "active tab" in permissions.

To pull the selected text from the active tab:

function getSelectedText(){
    chrome.tabs.executeScript({code: 'getSelection().toString()'}, 
    res => callback(res)  //res => { alert(res[0]) });
    );
}

var blah;
function callback(results){
    console.log(results);
    blah = results[0];
}

"blah" will contain the selected text in the calling process where you can work with it.

Thanks to wOxxOm for pointing me in the right direction and giving me a push.

Alex Russell
  • 189
  • 1
  • 10