Javascript Firefox WebExtension,在浏览器中存储一个数组';存储
您可以使用Javascript Firefox WebExtension,在浏览器中存储一个数组';存储,javascript,firefox-addon-webextensions,Javascript,Firefox Addon Webextensions,您可以使用browser.storage.local.set存储数组,还是使用其他方法实现相同的结果 详情: 我的扩展当前将重定向通过options.html表单指定的网站。当前,当您指定新网站时,旧网站将被替换。有没有一种方法可以附加到一系列将被重定向的网站,而不是替换网站 options.js:(将处理options.html中表单中的信息) redirect.js: function onError(error) { console.log(`Error: ${error}`);
browser.storage.local.set
存储数组,还是使用其他方法实现相同的结果
详情:
我的扩展当前将重定向通过options.html表单指定的网站。当前,当您指定新网站时,旧网站将被替换。有没有一种方法可以附加到一系列将被重定向的网站,而不是替换网站
options.js:(将处理options.html中表单中的信息)
redirect.js:
function onError(error) {
console.log(`Error: ${error}`);
}
function onGot(item) {
var url = "reddit.com";
if (item.url) {
url = item.url;
}
var host = window.location.hostname;
if ((host == url) || (host == ("www." + url))) {
window.location = chrome.runtime.getURL("redirect/redirect.html");
}
}
var getting = browser.storage.local.get("url");
getting.then(onGot, onError);
function onGot(item) {
var url = "";
if (item.url) {
url = item.url;
}
var host = window.location.hostname;
if ( (url.includes(host)) || (url.includes("www." + host)) ) {
window.location = chrome.runtime.getURL("redirect/redirect.html");
}
}
我的一个想法是为每个url添加一个存储位置,但是I
也必须存储,以防止每次加载options.js时重置它。(与下面的代码类似)
更合理的解决方案是将url
存储位置设置为数组
如果有办法将url
作为数组,则redirect.html可能包含以下内容:
if ( (url.includes (host) ) || (url.includes ("www." + host) ) ){
window.location = chrome.runtime.getURL("redirect.html");
}
新鲜的眼睛解决了我的问题 在options.js中:
function saveOptions(e) {
e.preventDefault();
var array = (document.querySelector("#url").value).split(",");
browser.storage.local.set({
url: array
});
在redirect.js中:
function onError(error) {
console.log(`Error: ${error}`);
}
function onGot(item) {
var url = "reddit.com";
if (item.url) {
url = item.url;
}
var host = window.location.hostname;
if ((host == url) || (host == ("www." + url))) {
window.location = chrome.runtime.getURL("redirect/redirect.html");
}
}
var getting = browser.storage.local.get("url");
getting.then(onGot, onError);
function onGot(item) {
var url = "";
if (item.url) {
url = item.url;
}
var host = window.location.hostname;
if ( (url.includes(host)) || (url.includes("www." + host)) ) {
window.location = chrome.runtime.getURL("redirect/redirect.html");
}
}
新鲜的眼睛解决了我的问题 在options.js中:
function saveOptions(e) {
e.preventDefault();
var array = (document.querySelector("#url").value).split(",");
browser.storage.local.set({
url: array
});
在redirect.js中:
function onError(error) {
console.log(`Error: ${error}`);
}
function onGot(item) {
var url = "reddit.com";
if (item.url) {
url = item.url;
}
var host = window.location.hostname;
if ((host == url) || (host == ("www." + url))) {
window.location = chrome.runtime.getURL("redirect/redirect.html");
}
}
var getting = browser.storage.local.get("url");
getting.then(onGot, onError);
function onGot(item) {
var url = "";
if (item.url) {
url = item.url;
}
var host = window.location.hostname;
if ( (url.includes(host)) || (url.includes("www." + host)) ) {
window.location = chrome.runtime.getURL("redirect/redirect.html");
}
}
对您是否尝试存储阵列?是否可能重复您认为无法存储阵列的原因?认真地我希望能够更改文档,这样其他人就不会有这种困惑。我正在试图找到可以改进文档的地方,以防止其他人产生这种印象。@Makyen我认为问题可能是眼睛疲劳。再次查看文档后,chrome()和mozilla()都清楚地说明了可以存储阵列。您是否尝试存储阵列?是否可能重复您认为无法存储阵列的原因?认真地我希望能够更改文档,这样其他人就不会有这种困惑。我正在试图找到可以改进文档的地方,以防止其他人产生这种印象。@Makyen我认为问题可能是眼睛疲劳。再次查看文档后,chrome()和mozilla()都清楚地说明了可以存储阵列。