繁体   English   中英

保存popup.html信息以用于下一个会话

[英]Save popup.html info for next session

我觉得这很简单,但我无法弄清楚。

我的应用程序基本上是通过执行完美工作的document.getElementById来对popup.html页面进行修改的。

[..]
document.getElementById("data").innerHTML = "inserting data here"
[..]

如预期的那样,当单击远离popup.html时,页面关闭。 当它打开备份时,修改不再存在。

我一直在查看背景页面https://developers.google.com/chrome/apps/docs/background?csw=1#example-permission尝试将其整理出来,但感觉不一样做我需要做的。

有人有什么建议吗?

谢谢!

像这样打开弹出窗口时,可以使用localStoragesessionStorage来存储值

localStorage.setItem('mydata', 'inserting data here')

当弹出窗口加载时,您可以从中获取值

document.getElementById("data").innerHTML = localStorage.getItem('mydata')

所以你的代码将是这样的

if(localStorage.getItem('mydata')) {
   document.getElementById("data").innerHTML = localStorage.getItem('mydata')
} else {
   localStorage.setItem('mydata', 'inserting data here')
}

如果弹出窗口与主页具有相同的域,则可以正常工作

您可以使用Chrome内置的存储API来完成此操作。

http://developer.chrome.com/extensions/storage.html

//在清单中

"permissions": [
    "storage"
  ],

//保存

function saveChanges() {
  // Get a value saved in a form.
  var theValue = textarea.value;
  // Check that there's some code there.
  if (!theValue) {
    message('Error: No value specified');
    return;
  }
  // Save it using the Chrome extension storage API.
  chrome.storage.local.set({'value': theValue}, function() {
    // Notify that we saved.
    message('Settings saved');
  });
}

//检索

chrome.storage.local.get('value', function(data) {
   // act upon data.
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM