简体   繁体   English

从网页Firefox扩展中复制数据

[英]Copy Data from webpage Firefox extension

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. 例如在Gmail上。 If I select anything from Gmail and try to find the selection it will end up with an error: 如果我从Gmail中选择任何内容并尝试找到该选择,则最终将出现错误:

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

Here is my code (This is a working example. I didn't include the icon.): 这是我的代码(这是一个有效的示例。我没有包含图标。):

manifest.json 的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 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 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. 它适用于Stack Overflow和其他网站,但不适用于使用更多框架等的网站(例如Gmail)。

Loop Image, as you can see it able to grab the text first time and then its keep sending the message I think. 循环图像,如您所见,它能够第一次捕获文本,然后继续发送我认为的消息。

在此处输入图片说明

What I am really missing? 我真正想念的是什么?

I did solved my issue using context menu item and it works very well with every where like iframes. 我确实使用上下文菜单项解决了我的问题,它在iframe之类的所有地方都非常有效。 I got a example from firefox repo. 我从Firefox回购中获得了一个例子。 https://github.com/mdn/webextensions-examples/tree/master/context-menu-demo . https://github.com/mdn/webextensions-examples/tree/master/context-menu-demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM