簡體   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