本地存储正在使用扩展名 [英] Local storage is being erased in chrome extension
本文介绍了本地存储正在使用扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当我关闭我的标签(3次后)或退出Chrome时,它会忘记我的选择。
My options.html
< html>
< head>
< script src =options.js>< / script>
< link href ='https://fonts.googleapis.com/css?family = Raleway:400,300,200,100'rel ='stylesheet'type ='text / css'>
< / head>
< body>
< center>
< label class =paylabelfor =cardtype>主题:< / label>
< select id =cardtypename =cards>
< option value =selectcard> ---请选择---< / option>
< option value =op1>主题1< / option>
< option value =op2>主题2< / option>
< / select>
< / center>
< br />
< br />
< br />
< br />
< h1 style =font-family:Raleway; font-weight:200; float:left> Theme 1< / h1>
< h1 style =font-family:Raleway; font-weight:200; float:Right> Theme 2< / h1>
< br />
< br />
< br />
< br />
< br />
< br />
< img src =screen2.png; alt =style =float:left/>
< img src =screen1.png; alt =style =float:right/>
< / body>
< / html>
Options.js:
time = setInterval(function(){
var ddl = document.getElementById(cardtype);
var selectedValue = ddl.options [ddl.selectedIndex] .value;
localStorage [inputText] = selectedValue;
var myvar = localStorage [inputText];
if(myvar ==op2)
{
chrome。 browserAction.setPopup({
popup:'popup.html'
});
}
else {
chrome.browserAction.setPopup({
popup :'popup2.html'
});
}
},1000);
谢谢。
解决方案
- 在结束<$ c之前移动
< script src =options.js>< / script>
$ c>< / body> 标记,以便脚本代码仅在整个文档被分析和加载时执行。 c> setInterval 使用适当的更改监听器。
b $ b
var element = document.getElementById(cardtype);
element.value = localStorage.inputText || OP1’ ;
element.addEventListener(change,function(e){
var selectedValue = this.value;
localStorage.inputText = selectedValue;
chrome.browserAction。 setPopup({
popup:selectedValue =='op1'?'popup.html':'popup2.html'
});
});
So I want to save the theme the user chooses, and accordingly I'm choosing the popup to show them. Every time I close my tab (after 3 times) or quit chrome, it forgets my choice.
My options.html
<html>
<head>
<script src="options.js"></script>
<link href='https://fonts.googleapis.com/css?family=Raleway:400,300,200,100' rel='stylesheet' type='text/css'>
</head>
<body>
<center>
<label class="paylabel" for="cardtype">Theme:</label>
<select id="cardtype" name="cards">
<option value="selectcard">--- Please select ---</option>
<option value="op1">Theme 1</option>
<option value="op2">Theme 2</option>
</select>
</center>
<br />
<br />
<br />
<br />
<h1 style="font-family:Raleway; font-weight:200; float:left">Theme 1</h1>
<h1 style="font-family:Raleway; font-weight:200; float:Right">Theme 2</h1>
<br />
<br />
<br />
<br />
<br />
<br />
<img src = "screen2.png"; alt="" style="float:left"/>
<img src = "screen1.png"; alt="" style="float:right"/>
</body>
</html>
Options.js:
time=setInterval(function(){
var ddl = document.getElementById("cardtype");
var selectedValue = ddl.options[ddl.selectedIndex].value;
localStorage["inputText"] = selectedValue;
var myvar = localStorage["inputText"];
if (myvar == "op2")
{
chrome.browserAction.setPopup({
popup: 'popup.html'
});
}
else{
chrome.browserAction.setPopup({
popup: 'popup2.html'
});
}
},1000);
Thanks.
解决方案
- Move
<script src="options.js"></script>
before the closing</body>
tag so that the script code will execute only when the entire document is parsed and loaded. - Instead of
setInterval
use a proper on-change listener.
var element = document.getElementById("cardtype");
element.value = localStorage.inputText || 'op1';
element.addEventListener("change", function(e) {
var selectedValue = this.value;
localStorage.inputText = selectedValue;
chrome.browserAction.setPopup({
popup: selectedValue == 'op1' ? 'popup.html' : 'popup2.html'
});
});
这篇关于本地存储正在使用扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文