简体   繁体   English

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

[英]Save popup.html info for next session

I feel like this is simple, but I can't figure it out. 我觉得这很简单,但我无法弄清楚。

My app basically makes modification to the popup.html page by doing a document.getElementById which works perfectly. 我的应用程序基本上是通过执行完美工作的document.getElementById来对popup.html页面进行修改的。

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

As expected, when clicking away from the popup.html, the page closes. 如预期的那样,当单击远离popup.html时,页面关闭。 When it opens back up, the modification is no longer there. 当它打开备份时,修改不再存在。

I've been looking at background pages https://developers.google.com/chrome/apps/docs/background?csw=1#example-permission to try and get that sorted out, but it doesn't feel like it would do what I'd need to. 我一直在查看背景页面https://developers.google.com/chrome/apps/docs/background?csw=1#example-permission尝试将其整理出来,但感觉不一样做我需要做的。

Does anyone have any recommendations ? 有人有什么建议吗?

Thanks! 谢谢!

You can use localStorage or sessionStorage to store the values when popup opens like this 像这样打开弹出窗口时,可以使用localStoragesessionStorage来存储值

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

and when popup loads you can fetch the value from it 当弹出窗口加载时,您可以从中获取值

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

so your code would be something like this 所以你的代码将是这样的

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

if the popup has same domain as the main page it will work fine 如果弹出窗口与主页具有相同的域,则可以正常工作

You can use Chromes built-in storage APIs to accomplish this. 您可以使用Chrome内置的存储API来完成此操作。

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

// In your manifest //在清单中

"permissions": [
    "storage"
  ],

// Saving //保存

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');
  });
}

// Retrieving //检索

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

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

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