无法在Chrome扩展程序的chrome.storage.sync中存储值

时间:2018-08-19 11:19:16

标签: javascript google-chrome-extension google-chrome-devtools

我无法使用chrome.storage.sync api来存储值,我正在尝试存储当前选项卡的选项卡ID,其中从popup.html中按下selectthistab按钮

这是我的manifest.json

 {
    "name": "Reload 4 U",
    "version": "0.1",
    "description": "Reload tab for you!",
    "permissions": ["activeTab","declarativeContent","storage","tabs","alarms"],
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "page_action": {
      "default_popup": "popup.html"
    },
    "options_page": "options.html",
    "icons": {
      "16": "images/get_started16.png",
      "32": "images/get_started32.png",
      "48": "images/get_started48.png",
      "128": "images/get_started128.png"
    },
    "manifest_version": 2
  }

这是我的popup.html

    <!DOCTYPE html>
  <html>
    <head>
      <style>
        button {

        }
      </style>
    </head>
    <body>
      <div>
        <button id="selectThisTab">Select This tab</button>
      </div>
      <script src="popup.js"></script>
    </body>
  </html>

这是我的popup.js

    let selectTab = document.getElementById('selectThisTab');

selectTab.onclick = function(element){
    function setTabId(tabs) {
        var currentTab = tabs[0];
        chrome.storage.sync.set({selectedTab: currentTab.id});
    }
var query = { active: true, currentWindow: true };
    chrome.tabs.query(query, setTabId);
    chrome.alarms.create('reloadTabAlarm', {delayInMinutes: 0.1, periodInMinutes: 0.2});
    var tabId = undefined;
     chrome.storage.sync.get('selectedTab',function (data) {
        tabId =  data.selectedTab;
    });
    alert("tab stored "+tabId);
};

这是我的background.js

    chrome.runtime.onInstalled.addListener(function() {
    function reloadTab(){
      console.log(chrome.storage.sync.get('selectedTab',function (data) {
        return data.selectedTab;
    }));
    }
    chrome.alarms.onAlarm.addListener(function(alarm) {
        reloadTab();
    });
});

我无法为chrome扩展程序存储任何数据,我正在尝试将Tab ID存储在存储设备中,manifest.json中提到了存储设备,但仍无法正常工作,请帮忙。

0 个答案:

没有答案