gpt4 book ai didi

javascript - PageMod 的 Firefox Addon Attach 事件未发出事件

转载 作者:行者123 更新时间:2023-12-01 05:38:28 24 4
gpt4 key购买 nike

我无法在我的插件和附加脚本之间进行通信。 PageMod 的附加事件有效,但它不会将我的 getElements 事件发送到我的列表 getter 。如何正确使用port.emit方法?谢谢!

index.js(插件)

var pageMod = require('sdk/page-mod');
var data = require("sdk/self").data;

pageMod.PageMod({
include: "*.workflowy.com",
contentScriptFile: [data.url("jquery-1.11.3.min.js"), data.url("list-getter.js")],
onAttach: function(worker) {

// For some reasong the worker doesn't emit?
worker.port.emit("getElements");

worker.port.on("gotElements", function(times) {
function printValues(el) {
console.log(el);
}
times.forEach(printValues);
});
}
});

list-getter.js(内容脚本文件)

self.port.on("getElements", function() {
var timeRegex = /\bam|pm/g
var openLists = $('.project.open').find('.content');
var times = []
for (var i = 0; i < openLists.length; i++) {
if (openLists[i].innerHTML.match(timeRegex)) {
times.push(openLists[i].innerHTML)
}
}
self.port.emit("gotElements", times);
});

最佳答案

我犯了一个令人难以置信的错误。确保将您想要由数据对象加载的任何内容放入数据文件夹中。我有两份副本,但正在更改错误的一份。谢谢。

index.js

var pageMod = require('sdk/page-mod');
var data = require("sdk/self").data;

pageMod.PageMod({
include: "*.workflowy.com",
contentScriptFile: data.url("list-getter.js"),
onAttach: function(worker) {

worker.port.emit("getElements");

worker.port.on("gotElement", function(times) {

function printValues(el) {
console.log(el);
}
times.forEach(printValues);
});
}
});

list-getter.js

self.port.on("getElements", function(openProject) {
var times = []
var timeRegex = /\b([1-9]pm|[1-9]am|1[0-2]am|1[0-2]pm)/g;
var openLists = document.querySelectorAll('.content');
for (var i = 0; i < openLists.length; i++) {
if (openLists[i].innerHTML != "") {
if (openLists[i].innerHTML.match(timeRegex)) {
times.push(openLists[i].innerHTML);
}
}
}
self.port.emit("gotElement", times);
)};

关于javascript - PageMod 的 Firefox Addon Attach 事件未发出事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32467040/

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