当您有'包括'

What is the best way to update/refresh a Page-Mod when you have new data for the 'Include'?

本文关键字:包括      更新时间:2023-09-26

我有一个page-mod,它使用数组作为include字段的数据。我希望能够使用splice从阵列中添加/删除数据,并刷新page-mod。这是我的代码:

//APIs:
var store = require("simple-storage");
var pageMod = require("page-mod");
var self = require("self");
//Array for Include:
store.storage.sites = ["*.example.com","*.google.com","*.org"];
//Page-mod code:
myPageMod = pageMod.PageMod({
  include: store.storage.sites,
  contentScriptWhen: 'start',
  contentScriptFile: [self.data.url("nicEdit-latest.js"),
                      self.data.url("pagedit.js")]
});
//Code to change array
function deleteItem(index) {
  store.storage.sites.splice(index,1);
});

现在,我需要的是一种将page-mod作为deleteItem的一部分进行更新的方法,这样它就不再适用于新删除的网站。

我已经尝试将page-mod放入函数中并调用该函数。我还尝试更新数组,然后更新myPageMod.include = store.storage.sites,但也没有成功。

page-mod有一个include属性,即当前包含规则的列表。您可以在上面调用add()remove()方法,例如:

function addItem(rule) {
  store.storage.sites.push(rule);
  myPageMod.include.add(rule);
}
function deleteItem(index) {
  myPageMod.include.remove(store.storage.sites.splice(index,1));
}

我认为如果你使用一个变量来引用你的pagemod,你应该能够更改它。

var myPageMod = pageMod.PageMod({
  include: store.storage.sites,
  contentScriptWhen: 'start',
  contentScriptFile: [self.data.url("nicEdit-latest.js"),
                      self.data.url("pagedit.js")]
});
function deleteItem(index) {
  myPageMod.include = store.storage.sites.splice(index,1);
});