0

I have a very strange situation. I have an extension which copies stuff from the webpage based on the user's selection. But, when ever there are multiple frames its fails. For example on Gmail. If I select anything from Gmail and try to find the selection it will end up with an error:

Error: window.getSelection(...) is null

Here is my code (This is a working example. I didn't include the icon.):

manifest.json

{
    "description": "Adds a solid red border to all webpages matching mozilla.org. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#borderify",
    "manifest_version": 2,
    "name": "Borderify",
    "version": "1.0",
    "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/borderify",
    "icons": {
        "48": "icons/border-48.png"
    },
    "background": {
        "scripts": ["myaddone.js"]
    },
    "browser_action": {
        "default_icon": "icons/trash.svg",
        "default_title": "Forget it!"
    },
    "content_scripts": [{
        "matches": ["<all_urls>"],
        "js": ["callHere.js"],
        "all_frames": true
    }]
}

callHere.js

function logger(msg) {
    console.log("=============");
    console.log(msg);
    console.log("=============");
}
var getSelectedDataFromPage = function () {
    logger("fooo");
    selec = window.getSelection().toString().trim();
    return selec;
}
browser.runtime.onMessage.addListener(request => {
    var messageToCopy = request.greeting;
    if (messageToCopy == "findCopy") {
        var selectedText = getSelectedDataFromPage();
        return Promise.resolve({
            response: selectedText
        });
    }
    logger(messageToCopy);
    return Promise.resolve({
        response: "Fail"
    });
});

myaddone.js

function logger(msg) {
    console.log(msg);
}

function onError(error) {
    console.error(`Error: ${error}`);
}

function findSelectionTExt(tabs) {
    for (let tab of tabs) {
        browser.tabs.sendMessage(tab.id, {
            greeting: "findCopy"
        }).then(response => {
            logger(response.response);
        }).catch(onError);
    }
}
browser.browserAction.onClicked.addListener(() => {
    browser.tabs.query({
        currentWindow: true,
        active: true
    }).then(findSelectionTExt).catch(onError);
});

It is using a message system to content script to copy stuff from selection. It works perfectly fine with Stack Overflow and other sites, but not sites which use more frames etc., like Gmail.

Loop Image, as you can see it able to grab the text first time and then its keep sending the message I think.

enter image description here

What I am really missing?

9
  • The code you are using to get the selection is insufficient. Use the more complex code which you can find in the snippet in: Get the Highlighted/Selected text. As you should be able to test, the code you are using will fail to get the selection in Firefox under some conditions. – Makyen Aug 12 '17 at 7:17
  • Please, use one indenting style consistently throughout your code. Doing so makes it much easier to read/maintain. As a side effect, doing so for code you place on Stack Overflow makes it much more likely that you will get people to up-vote your posts and makes it more likely that people will put time into Answering your Questions. It doesn't matter which style your choose (although, IMO, some are more appropriate for JavaScript than others). But, pick one and use it consistently for all code in a single project. – Makyen Aug 12 '17 at 7:19
  • Adjusting your code to include the more robust, and necessary, code at the link I provided may, or may not, fix your specific issue. It's not clear if that is your actual issue, or not. You have provided too little information about the circumstances in which you are using this for us to actually know. However, not using the more robust code will cause issues with not getting the selected text. Please include the more robust code and retest. – Makyen Aug 12 '17 at 7:30
  • I am pretty sure my code indenting style is fine and I am sure you didn't even tried to run this code .. its very basic and copied from firefox git repo. As I told my issue is it is not working where frames is used. It going some loop which I can't find where, other site like SO it works fine. Even the same code works fine in chrome even with gmail. I attached the trace back image. – Achayan Aug 12 '17 at 17:03
  • Althoguh I tried the solution which you mentioned in your first comment. Which also won't work. – Achayan Aug 12 '17 at 17:08
0

I did solved my issue using context menu item and it works very well with every where like iframes. I got a example from firefox repo. https://github.com/mdn/webextensions-examples/tree/master/context-menu-demo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.