Chrome扩展程序浏览器操作仅适用一次

问题描述 投票:0回答:1

当用户单击浏览器图标时,应运行spiderSimulator()。我可以让它上班 - 一次。

就像,我会点击图标,它会做它应该做的事情。但是,如果我刷新页面并再次单击它,则不起作用。如果我去另一个网站再试一次,它就行不通了。

如果我更新扩展,刷新页面并清除缓存,它会再次起作用 - 有时候。这是超级不一致的。

知道这里出了什么问题吗?

content.js

// Adds an event listener to the browser icon to sends a message to background.js.
chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
        if (request.message === "clicked_browser_action") {
            console.log( 'send message' );
            chrome.runtime.sendMessage({"message": "spider_simulator"});
        }
    }
);

background.js

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function (tab) {
    // Send a message to the active tab
    chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
        var activeTab = tabs[0];
        console.log('browser action');
        chrome.tabs.sendMessage(activeTab.id, {"message": "clicked_browser_action"});
    });
});

chrome.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
        if (request.message === "spider_simulator") {
            console.log('run the spider');
            spiderSimulator()
        }
    }
);

// removes the site's stylesheet and replaces it with my own; 
// turns all page elements into nested list items. 
function spiderSimulator() {

    console.log( 'inject stylesheet' );
    chrome.tabs.insertCSS({
        file: 'search-spider-simulator.css'
    });

    console.log( 'execute script' );
    chrome.tabs.executeScript({
        file: 'search-spider-simulator.js'
    });

    window.close();
}
javascript google-chrome-extension
1个回答
0
投票

正如wOxxOm所提到的,在后台页面中执行的window.close()会自行处理 - 基本上会终止扩展的后台部分。

不确定那是什么意思;如果你需要关闭你正在谈话的标签,那么这也需要executeScript'ed。

© www.soinside.com 2019 - 2024. All rights reserved.