保存弹出窗口.html信息以供下一个会话使用



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

我的应用程序基本上通过执行一个完美运行的document.getElementById来修改弹出窗口.html页面。

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

正如预期的那样,当单击离开弹出窗口.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.
});

相关内容

最新更新