Chrome扩展程序:仅当background.js错误

时间:2019-03-13 10:38:09

标签: javascript google-chrome-extension popup popup.js

我一直在努力解决这个问题,但到目前为止还没有运气。

我要做的是单击扩展图标上的要进行检查时。如果该检查失败,我希望显示弹出窗口,否则将其隐藏。然后,每次在扩展图标上单击时,都会重新运行检查,以查明当前选项卡是否通过检查。

第一次点击的效果很好,但随后的点击需要两次才能生效。

这是我目前拥有的:

background.js:

// Stop the popup displaying on first load
chrome.browserAction.setPopup({popup: ""});

// Check whether the main.css can load, if not the setup the popup.html to display
chrome.tabs.executeScript (null, {file: "main.css"}, function() {
    if (chrome.runtime.lastError) {
        // The file couldn't be loaded to display the popup
        chrome.browserAction.setPopup({popup: 'popup.html'});
    }
});


chrome.browserAction.onClicked.addListener(function(tab) {

     chrome.tabs.executeScript (null, {file: "main.js"}, function() {
        if (chrome.runtime.lastError) {
           var errorMsg = chrome.runtime.lastError.message

           console.log('Error occurred... ' + errorMsg);

           // Show the popup as there has been an error
           chrome.browserAction.setPopup({popup: 'popup.html'});
        }
        else
        {
           // Everything loaded ok so now load the css file too
           console.log('All ok...');

           // Stop the popup appearing
           chrome.browserAction.setPopup({popup: ""});
           chrome.tabs.insertCSS(null, {file: "main.css"});   
        }
    });
});

popup.js:

chrome.browserAction.setPopup({popup: ""});

// Run this within popup so the a bespoke message can be displayed and that the popup can be made to work in the same way as the chrome.browserAction.onClicked event in background.js
// This is because 'onClicked' event does not fire if the browser action has a popup: https://developer.chrome.com/extensions/browserAction#event-onClicked
chrome.tabs.executeScript (null, {file: "main.js"}, function() {
    if (chrome.runtime.lastError) {
      var aErrorMessage = chrome.runtime.lastError.message

      chrome.browserAction.setPopup({popup: 'popup.html'});

      // Create a div to put the Error Message in
      var aErrorMessageContainer = document.createElement('div');
      aErrorMessageContainer.setAttribute('id', 'errormsg-container');

      document.body.appendChild(aErrorMessageContainer);

      // Create a paragraph to place the error message text in
      var aErrorMessagePara = document.createElement('p');
      aErrorMessagePara.setAttribute('id', 'error-message');
      aErrorMessagePara.innerHTML = '<hr/><b>Chrome Error Message:</b><br /> <i>' + aErrorMessage + '</i>';

      aErrorMessageContainer.appendChild(aErrorMessagePara);
    }
    else
    {
      chrome.browserAction.setPopup({popup: ""});
      chrome.tabs.insertCSS(null, {file: "main.css"});   
    }
});

这里的问题是我试图设置弹出窗口以显示或隐藏在“ chrome.browserAction.onClicked”事件中的部分。

background.js顶部的部分可以很好地决定是最初显示还是隐藏弹出窗口,因此在那里没有问题。

但是,如果用户所在的选项卡在background.js的“ onClicked”事件中未通过“ chrome.tabs.executeScript”测试,则用户随后导航至浏览器中不会失败的其他选项卡“ chrome.tabs.executeScript”测试必须单击两次以查看结果。

我认为这是因为尽管在Click事件中设置了弹出值,但是直到用户第二次单击扩展程序的图标,一切都没有改变。

我希望这是有道理的!

任何帮助将不胜感激。

0 个答案:

没有答案
相关问题