Javascript Chrome扩展不';t在重新加载之前考虑optionvalue
我正在做我的第一个Chrome扩展。多亏了“Rob W” 实际上,扩展使用一些参数对远程页面执行XMLHttpRequest,并在处理结果后,将html列表呈现到弹出窗口中 现在一切都启动并运行了,所以我要添加一些选项。 第一个是“要加载多少个元素”,用于对列表中的元素设置限制 我正在使用花哨的设置来管理我的选项,这就是问题所在 扩展的行为就像本地存储设置有一个“缓存”。 如果我没有设置任何内容并执行扩展的干净安装,则正确加载默认数量的元素 如果我更改值。我需要重新加载扩展以查看更改。 只有在移除设置后,我才能立即看到扩展按预期工作 现在,我将更详细地介绍一些具体信息 这是popup.js脚本:Javascript Chrome扩展不';t在重新加载之前考虑optionvalue,javascript,google-chrome-extension,xmlhttprequest,local-storage,Javascript,Google Chrome Extension,Xmlhttprequest,Local Storage,我正在做我的第一个Chrome扩展。多亏了“Rob W” 实际上,扩展使用一些参数对远程页面执行XMLHttpRequest,并在处理结果后,将html列表呈现到弹出窗口中 现在一切都启动并运行了,所以我要添加一些选项。 第一个是“要加载多少个元素”,用于对列表中的元素设置限制 我正在使用花哨的设置来管理我的选项,这就是问题所在 扩展的行为就像本地存储设置有一个“缓存”。 如果我没有设置任何内容并执行扩展的干净安装,则正确加载默认数量的元素 如果我更改值。我需要重新加载扩展以查看更改。 只有在移
chrome.extension.sendRequest({action: 'gpmeGetOptions'}, function(theOptions) {
//Load the limit for topic shown
console.log('NGI-LH -> Received NGI "max_topic_shown" setting ('+theOptions.max_topic_shown+')');
//Initializing the async connection
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://gaming.ngi.it/subscription.php?do=viewsubscription&pp='+theOptions.max_topic_shown+'&folderid=all&sort=lastpost&order=desc');
xhr.onload = function() {
var html = "<ul>";
var doc = xhr.response;
var TDs = doc.querySelectorAll('td[id*="td_threadtitle_"]');
[].forEach.call(TDs, function(td) {
//Removes useless elements from the source
var tag = td.querySelector('img[src="images/misc/tag.png"]'); (tag != null) ? tag.parentNode.removeChild(tag) : false;
var div_small_font = td.querySelector('div[class="smallfont"]'); (small_font != null ) ? small_font.parentNode.removeChild(small_font) : false;
var span_small_font = td.querySelector('span[class="smallfont"]'); (small_font != null ) ? small_font.parentNode.removeChild(small_font) : false;
var span = td.querySelector('span'); (span != null ) ? span.parentNode.removeChild(span) : false;
//Change the look of some elements
var firstnew = td.querySelector('img[src="images/buttons/firstnew.gif"]'); (firstnew != null ) ? firstnew.src = "/img/icons/comment.gif" : false;
var boldtext = td.querySelector('a[style="font-weight:bold"]'); (boldtext != null ) ? boldtext.style.fontWeight = "normal" : false;
//Modify the lenght of the strings
var lenght_str = td.querySelector('a[id^="thread_title_"]');
if (lenght_str.textContent.length > 40) {
lenght_str.textContent = lenght_str.textContent.substring(0, 40);
lenght_str.innerHTML += "<span style='font-size: 6pt'> [...]</span>";
}
//Removes "Poll:" and Tabulation from the strings
td.querySelector('div').innerHTML = td.querySelector('div').innerHTML.replace(/(Poll)+(:)/g, '');
//Modify the URL from relative to absolute and add the target="_newtab" for the ICON
(td.querySelector('a[id^="thread_title"]') != null) ? td.querySelector('a[id^="thread_title"]').href += "&goto=newpost" : false;
(td.querySelector('a[id^="thread_goto"]') != null) ? td.querySelector('a[id^="thread_goto"]').href += "&goto=newpost": false;
(td.querySelector('a[id^="thread_title"]') != null) ? td.querySelector('a[id^="thread_title"]').target = "_newtab": false;
(td.querySelector('a[id^="thread_goto"]') != null) ? td.querySelector('a[id^="thread_goto"]').target = "_newtab": false;
//Store the td into the main 'html' variable
html += "<li>"+td.innerHTML+"</li>";
// console.log(td);
});
html += "</ul>";
//Send the html variable to the popup window
document.getElementById("content").innerHTML = html.toString();
};
xhr.responseType = 'document'; // Chrome 18+
xhr.send();
});
正如我所说,console.log显示缓存的值。
如果我将该值设置为“20”,它将保持默认值,直到我重新加载扩展。
如果我将其更改为30,它将保持在20,直到我重新加载扩展
如果还需要什么,就问吧。我将编辑这个问题。这个问题似乎是概念上的误解。Chrome扩展中的
background.js
脚本加载一次并继续运行,直到该扩展或Chrome浏览器重新启动
这意味着在当前代码中,仅当扩展首次启动时才会加载设置
变量值。为了访问自加载扩展名以来已更新的值,必须重新加载background.js
中的设置
变量值
有许多方法可以实现这一点。最简单的方法是将设置
相关代码移动到后台.js
中的chrome.extension.onRequest.addListener
回调函数中。这也是最低效的解决方案,因为每次请求都会重新加载设置,而不管它们是否已实际更新
更好的解决方案是,只有在选项页面中更新值时,才在background.js
中重新加载设置
值。这将使用设置变量的持久性或缓存来为您带来好处。您必须查看文档以了解实现细节,但我们的想法是从选项页面向background.js
页面发送一条消息,告诉它在存储新设置后更新settings
另一方面,行
var settings=settings.toObject()中的var
关键字代码>是不需要的。无需重新声明变量,它已在上面声明。感谢您的澄清。稍后我会检查(实际上工作中超载),并尽快给你一个“接受的答案”:
//Initialization fancy-settings
var settings = new Store("settings", {
"old_logo": false,
"max_topic_shown": "10"
});
//Load settings
var settings = settings.toObject();
//Listener who send back the settings
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.action == 'gpmeGetOptions') {
sendResponse(settings);
}
});