如何在chrome扩展中重新加载浏览器后保留变量的值 [英] How can I keep the value of variable after reload browser in chrome extensions
问题描述
我的目标是将URL和电子邮件发送到PHP服务器,但现在我将它们发送到控制台。
我的问题是保存用户输入到我的分机的最后一封电子邮件。
现在,当我关闭浏览器时,电子邮件的值将变为未定义。
我把我的扩展的整个代码放进去,但我认为问题必须是
$ p> //变量defenitions:
var email;
var urll;
//函数:
函数getemail(){
email = window.prompt(请输入您的电子邮件地址:);
chrome.storage.sset.set({'email':email},function(){
alert('You entered \\\
['+ email +] \\\
as Email address。 \感谢您的注册。);
});
var id = chrome.contextMenus.create({title:提交你的网址,onclick:consollog});
var id = chrome.contextMenus.create({title:email +/ Change,onclick:getemail});
chrome.contextMenus.remove(1,null);
chrome.contextMenus.remove(2,null);
var id = chrome.contextMenus.create({id:2,title:SUPPORT,onclick:support});
函数create(){
var id = chrome.contextMenus.create({title:提交您的网址,onclick: consollog});
var id = chrome.contextMenus.create({title:email +/ Change,onclick:getemail});
chrome.contextMenus.remove(1,null);
chrome.contextMenus.remove(2,null);
var id = chrome.contextMenus.create({id:2,title:SUPPORT,onclick:support});
}
function support(){
alert(联系方式:\\\
Phone number:+ 989378114692\\\
Email address:amukiarash@gmail.com);
函数consollog(info,tab){
chrome.tabs.query({
'active':true,currentWindow:true,
$ {
urll = lTabs [0] .url;
console.log(urll)
console.log(email)
alert('You发送'+ urll +'到我们的服务器');
});
}
//菜单项:
if(email == undefined){
var id = chrome.contextMenus.create({id:1,title :注册邮件,onclick:getemail});
}
else {create();}
var id = chrome.contextMenus.create({id:2,title: 载体, 点击:支持});
manifest.json:
{
name:Jenco,
description:此扩展可以帮助您发送url,
version:0.9,
browser_action:{
default_icon:32.png,
default_popup:mypopup.html},
permissions:[contextMenus ,tabs,activeTab,http:// * / *,https:// * / *,storage],
background:{
scripts :[background.js]
},
manifest_version:2,
图标:{16:16.png,
32 :32.png,
48:48.png},
commands:{
send-url:{
suggested_key: {default:Ctrl + Shift + Z},
description:发送一个url
}
}
}
解决方案我的代码保存一个变量并加载一个变量:
//保存
chrome.storage。 sync.set({variableName:value});
//加载1件事
chrome.storage.sync.get(variableName,function(result){
//显示请求的变量值
alert(result.variableName );
});
//加载更多东西
chrome.storage.sync.get([variableName,variableNameTheSecond],function(result){
//先显示第一个,然后显示第二个
alert(result.variablename);
alert(result.variableNameTheSecond);
});
我希望这可以帮助你,迎接
Ebbe
I am writing my first chrome extension and I am kind of new in this field.
My goal is sending url and email to a PHP server, but for now I send them to console.
My problem is saving the last email that user has entered to my extension. Right now when I close the browser the value of email will change to undefined.
I put entire code of my extension, but I think the problem must be in
getemail
function.background.js
//variables defenitions: var email; var urll; //functions: function getemail(){ email = window.prompt("Pleas enter your Email address:"); chrome.storage.sync.set({'email':email}, function() { alert('You entered \n[' + email + "]\n as Email address.\nThank you for registration." ); }); var id = chrome.contextMenus.create({"title": "Submit your url","onclick":consollog }); var id = chrome.contextMenus.create({"title": email+"/Change" ,"onclick":getemail }); chrome.contextMenus.remove("1", null); chrome.contextMenus.remove("2", null); var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support}); } function create(){ var id = chrome.contextMenus.create({"title": "Submit your url","onclick":consollog }); var id = chrome.contextMenus.create({"title": email+"/Change" ,"onclick":getemail }); chrome.contextMenus.remove("1", null); chrome.contextMenus.remove("2", null); var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support}); } function support(){ alert("Contact ways:\nPhone number:+989378114692\nEmail address:amukiarash@gmail.com"); } function consollog(info, tab) { chrome.tabs.query({ 'active':true,"currentWindow": true, }, function(lTabs) { urll=lTabs[0].url; console.log(urll) console.log(email) alert('You sent "' + urll + '" to our server'); }); } //menu items: if(email==undefined){ var id = chrome.contextMenus.create({"id":"1","title": "Register Email","onclick":getemail }); } else {create();} var id = chrome.contextMenus.create({"id":"2","title": "SUPPORT","onclick":support});
manifest.json:
{ "name": "Jenco", "description": "This extension helps you to send url", "version": "0.9", "browser_action":{ "default_icon":"32.png", "default_popup":"mypopup.html"}, "permissions": ["contextMenus","tabs","activeTab", "http://*/*", "https://*/*","storage"], "background": { "scripts": ["background.js"] }, "manifest_version": 2, "icons": { "16": "16.png", "32": "32.png", "48": "48.png" }, "commands": { "send-url": { "suggested_key": { "default": "Ctrl+Shift+Z" }, "description": "Send an url" } } }
解决方案My codes to save a variable and to load a variable:
// Saving chrome.storage.sync.set({"variableName": value}); // Loading 1 thing chrome.storage.sync.get("variableName", function(result){ // Showing the requested variable value alert(result.variableName); }); // Loading more things chrome.storage.sync.get(["variableName", "variableNameTheSecond"], function(result){ // Showing first the first one and then the second one alert(result.variablename); alert(result.variableNameTheSecond); });
I hope this helps you, greets
Ebbe
这篇关于如何在chrome扩展中重新加载浏览器后保留变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!