gpt4 book ai didi

javascript - Firefox 附加 pageshow 事件在工作人员能够接收消息之前触发

转载 作者:行者123 更新时间:2023-11-28 08:02:49 29 4
gpt4 key购买 nike

我正在编写一个 Firefox 插件并在数组中跟踪每个页面的工作人员。除了管理此阵列所需的一些花哨的步法(如此处所述 https://bugzilla.mozilla.org/show_bug.cgi?id=686035 和此处 Addon SDK - context-menu and page-mod workers ),一切都正常工作。我遇到的一个问题是,当监听选项卡 pageshow 事件(或工作人员自己的 pageshow 事件)时,回调似乎在工作人员之前触发实际上已经准备好了。在回调中检索页面相应的工作线程并使用它尝试向内容脚本发送消息时,我收到错误页面当前处于隐藏状态,在再次可见之前无法再使用。 通常情况下,我只会使用 setTimeout 并咬紧牙关,但这不适用于附加组件。什么是合适的解决方法?该附加组件主要部分的代码如下:

var { ToggleButton } = require('sdk/ui/button/toggle');
var panels = require('sdk/panel');
var tabs = require('sdk/tabs');
var self = require('sdk/self');
var pageMods = require('sdk/page-mod');
var ss = require('sdk/simple-storage');
var workers = [];

ss.storage.isPluginActive = ss.storage.isPluginActive || false;

var button = ToggleButton({
id: 'tomorrowww',
label: 'Tomorowww',
icon: {
'16': './icon-16.png',
'32': './icon-32.png',
'64': './icon-64.png'
},
onChange: handleButtonChange
});

var panel = panels.Panel({
contentURL: self.data.url('panel.html'),
contentScriptFile: self.data.url('panel-script.js'),
onHide: handlePanelHide,
width: 342,
height: 270
});

panel.port.on('panel-ready', handlePanelReady);
panel.port.on('plugin-toggled', handlePluginToggled);
panel.port.on('link-clicked', handleLinkClicked);

pageMods.PageMod({
include: ['*'],
contentScriptFile: [self.data.url('CancerDOMManager.js'), self.data.url('content-script.js')],
contentStyleFile: self.data.url('content-style.css'),
onAttach: function (worker) {
addWorker(worker);
sendActiveState(ss.storage.isPluginActive);
}
});

// move between tabs
tabs.on('activate', function () {
sendActiveState();
});

// this actually fires before the worker's pageshow event so isn't useful as the workers array will be out of sync
//tabs.on('pageshow', function () {
// sendActiveState();
//});

function addWorker (worker) {
if(workers.indexOf(worker) > -1) {
return;
}

worker.on('detach', handleWorkerDetach);
worker.on('pageshow', handleWorkerShown);
worker.on('pagehide', handleWorkerHidden);
workers.push(worker);
}

function handleWorkerDetach () {
removeWorker(this, true);
}

function handleWorkerShown () {
addWorker(this);

// back / forward page history
// trying to send the state here will trigger the page hidden error
sendActiveState();
}

function handleWorkerHidden () {
removeWorker(this);
}

function removeWorker (worker, removeEvents) {
var index = workers.indexOf(worker);

removeEvents = removeEvents || false;

if(index > -1) {
if(removeEvents) {
worker.removeListener('detach', handleWorkerDetach);
worker.removeListener('pageshow', handleWorkerShown);
worker.removeListener('pagehide', handleWorkerHidden);
}

workers.splice(index, 1);
}
}

function getWorkersForCurrentTab () {
var i;
var tabWorkers = [];

i = workers.length;

while(--i > -1) {
if(workers[i].tab.id === tabs.activeTab.id) {
tabWorkers.push(workers[i]);
}
}

return tabWorkers;
}

function handlePanelReady () {
setActive(ss.storage.isPluginActive);
}

function setActive (bool) {
ss.storage.isPluginActive = bool;
panel.port.emit('active-changed', bool);
sendActiveState();
}

function sendActiveState () {
var tabWorkers = getWorkersForCurrentTab();
var i = tabWorkers.length;

while(--i > -1) {
tabWorkers[i].port.emit('toggle-plugin', ss.storage.isPluginActive);
}
}

function handleButtonChange (state) {
if(state.checked) {
panel.show({
position: button
});
}
}

function handlePanelHide () {
button.state('window', {checked: false});
}

function handleLinkClicked (url) {
if(panel.isShowing) {
panel.hide();
}

tabs.open(url);
}

function handlePluginToggled (bool) {
if(panel.isShowing) {
panel.hide();
}

setActive(bool);
}

最佳答案

尝试在 page-mod 中使用 contentScriptWhen: "start"

关于javascript - Firefox 附加 pageshow 事件在工作人员能够接收消息之前触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25183175/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com