首页 > 解决方案 > `chrome.storage.sync.get (['auth'] ... `content.js` 中的 `Alert` 返回一个空对象

问题描述

通过输入 www.app.com 我想访问auth本地存储中的密钥并将其显示在警报中。在content.js我使用的文件chrome.storage.sync.get中。当我访问 www.app.com 时,alert出现了一个,但它的内容只是一个空对象{},并且应该包含auth密钥的内容。

chrome.storage.sync.get(['auth'], function(result) {
  alert(JSON.stringify(result));
});

在本地存储中,我有 key: auth

{access_token:"1234567"
  expires_in: 86400
  refresh_token: "8906789"
  scope: null
  token_type: "Bearer"
}

内容.js

chrome.storage.sync.get(['auth'], function(result) {
  alert(JSON.stringify(result));

  chrome.runtime.sendMessage(item, function(response) {
    console.log(response);
  });
});

清单.json

{
  "name": "EXTENSION",
  "options_page": "options.html",
  "background": {
    "page": "background.html"
  },

  "content_scripts": [{
    "matches": [ "*://*.app.com/*"  ],
    "js": [ "content.js" ],
    "all_frames": true
}],
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": "icon-34.png"
  },
  "icons": {
    "128": "icon-128.png"
  },
  "permissions": [
   "https://*/",
   "http://*/",
   "*://*.app.com/*",
    "storage"
  ],
  "version": "1.0",
  "manifest_version": 2,
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}

标签: javascriptgoogle-chrome-extension

解决方案


推荐阅读