【问题标题】:chrome extension - opening multiple tabs won't open all tabschrome 扩展 - 打开多个标签不会打开所有标签
【发布时间】:2014-06-13 12:03:17
【问题描述】:

我创建了一个扩展,它会在弹出窗口中打开所有在 textarea 中输入的 URL。在最新的 chrome 版本中,它无法正常工作 - 有时它会打开所有输入的选项卡,但有时只打开其中的一部分。

我找到了一些信息,脚本需要移动到 background.js,这应该可以解决这个问题,但我不知道该怎么做。你能帮帮我吗?

manifest.js

{"name": "URL Opener",
"description": "",
"version": "1.0",
"icons": { "128": "url_128x128.png" },
"permissions": ["tabs", "http://*/*", "https://*/*", "background"],
"browser_action": {
"default_title": "URL Opener+",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"background": {"scripts": ["background.js"],"persistent": false
},
"manifest_version": 2
}

popup.html

这里我只有那个文本区域和点击按钮

popup.js

document.addEventListener('DOMContentLoaded', function () {var divs1 = document.getElementById("button");divs1.addEventListener('click', URLopener);});

function URLopener () {var txt = document.getElementById("textfield").value; 
for(i = 0; i < txt.length; i++){chrome.tabs.create({url: txt[i]});} 

}

到目前为止它运行良好,但现在它不会总是打开所有标签...

我尝试将函数URLopener()移到background.js,但不知道具体怎么从popup.js调用,或者还有什么办法……

【问题讨论】:

    标签: javascript google-chrome


    【解决方案1】:

    有几种方法可以做到这一点。

    这是一个(消息传递):

    popup.js 中发送 background.js 消息,其中包含您网站的一些数据结构。

    chrome.runtime.sendMessage({
        urls: ['http://example.url.whatever.com',
               'http://another.example.of.url.com']
    });
    

    然后,在 background.js 中处理消息。

    chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
            if (request.urls) {
                //request.urls is the array of urls from popup.js
            }
        }
    );
    

    请在此处阅读更多文档: https://developer.chrome.com/extensions/messaging

    【讨论】:

    • 感谢您的回复,我会研究这个的!但是,现在我通过 "active":false 解决了这个问题 - 我只是在这里 chrome.tabs.create({"active":false, url: txt[i]});而且效果很好,所以我不需要使用背景。它导致不专注于打开标签,因此它每次都会打开所有标签。
    猜你喜欢
    • 2016-06-19
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 2015-05-17
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多