【问题标题】:Script loading order in Google Chrome extensionGoogle Chrome 扩展程序中的脚本加载顺序
【发布时间】:2017-03-30 05:35:22
【问题描述】:

我是 chrome 扩展编码的新手,目前正在尝试构建一个扩展作为一种爱好

这是我的 manifest.json

    {
        "manifest_version": 2,
        "name": "LOL",
        "version": "0.1",
        "background": {
            "persistent": false,
            "scripts": ["background.js", "back.js"]
        },
        "browser_action": {
            "default_icon": "icon.png",
            "default_popup": "popup.html"
        },
        "permissions": [
            "downloads", "downloads.open", "downloads.shelf", "activeTab", "background"
        ]
    }

我在 popup.html 中有两个单选按钮。点击时,我正在执行 background.js 和 back.js。

但是,即使我选择了第二个单选按钮,background.js 仍在执行。

我观察到的奇怪行为是:如果我将 manifest.json 中的脚本顺序交换为 back.js,background.js.. 无论我选择什么单选按钮,只有 back.js 正在执行。

我不确定我的脚本有什么问题。请提出问题可能是什么。

这是我的 popup.js

    function hello() {
        chrome.tabs.executeScript({
            file: 'background.js'
        });}

        function hello2() {
            chrome.tabs.executeScript({
                file: 'back.js'
            });
        }


    var radio1 = document.getElementById('option1').value;
    if (radio1 == "folder1") {
        document.getElementById('option1').addEventListener('click', hello);
    }
    if (radio1 == "folder2") {
        document.getElementById('option2').addEventListener('click', hello2);
    }

这是 background.js

function alertlol() {

chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
suggest({filename: "mysubdirectory/" + item.filename});
});

}
chrome.browserAction.onClicked.addListener(alertlol);
alertlol();

这是 back.js

function alertlol1() {

chrome.downloads.onDeterminingFilename.addListener(function(item, suggest) {
suggest({filename: "mydir/" + item.filename});
});

}
chrome.browserAction.onClicked.addListener(alertlol1);
alertlol1();

这是 popup.html

<!DOCTYPE html>
<html>
  <body style="width: 300px">
    Select download folder
    </br><input type="radio"  name="option1" onclick="background.js" value="folder1">Download folder one </input>
    <script type="text/javascript" src="popup.js"></script></br>
    <input type="radio"  name="option1" onclick="back.js" value="folder2">Download folder two </input>
    <script type="text/javascript" src="popup.js"></script>
  </body>
</html>

这里是代码库

https://github.com/beardlessgenius/DLM

【问题讨论】:

  • background.js 和 back.js 中有什么?
  • 将相同的脚本用作后台脚本(清单)和内容脚本 (tabs.executeScript())(back.js 和 background.js )。您的代码有一个错误,该错误应该出现在您的弹出窗口的控制台中(即,当您尝试将其添加为侦听器时,未定义 hello2 的引用错误)。请参阅console for the popup。
  • 你的 popup.html 是?还有 back.js 和 background.js.
  • @KrishnaChaitanya,请edit 将内容放入问题中。虽然 cmets 中的信息可能会有所帮助,但回答问题所需的所有信息都应包含在问题中。您可以在 cmets 中复制它,但问题是主要的。
  • 好的,请解释一下这里发生了什么。您有两个文件,您声明为 background scripts,并从弹出窗口中尝试将它们作为 content scripts 注入,而其中的代码与内容脚本不兼容.您是否试图简单地执行代码,而不是注入内容脚本?如果是,在哪种情况下 - 背景?

标签: google-chrome-extension manifest.json


【解决方案1】:

我可以建议的一件事是, a) 仅使用一次&lt;script type="text/javascript" src="popup.js"&gt;&lt;/script&gt;,并在 HTML 的开头、头部或正文标记之后。 b) 在 popup.js 中,使用 onclick() 事件,而不是检查值。这是因为它只检查一次值并完成它。如果您稍后单击或单击同一个按钮两次怎么办?

在 html 中添加“onclick”事件后,如果问题仍然存在,请 ping 我。这只是一个开始,请从这里开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多