【问题标题】:Error in response to storage.get: TypeError: Cannot read property 'dataset' of null响应 storage.get 时出错:TypeError:无法读取 null 的属性“数据集”
【发布时间】:2017-04-24 17:12:15
【问题描述】:

在我的options.html 文件中,我在chrome.storage.local 中设置了一个值,但是当我尝试从我的弹出窗口中设置时,我无法get() 的值。 html。就像它无法获取它,因为它位于“不同”站点(弹出站点)上。这就是我得到的:

Error in response to storage.get:
TypeError: Cannot read property 'dataset' of null
    at Object.callback (chrome-extension://fcbadpjebgjnohhcihmkopdbnbjjmnod/load_id_to_popup.js:8:37)
    at chrome-extension://fcbadpjebgjnohhcihmkopdbnbjjmnod/load_id_to_popup.js:2:22

这不是错字,对吧?请告诉我,这很容易修复。

(First post (solved))

options.html

<!DOCTYPE html>
<html>
  <head>
   <title>Teamsepak 3 Server Viewer Options</title>
  <style>
    body: { padding: 10px; }
  </style>

    <title></title>
  </head>
  <body>
    <p>ENTER YOUR ID FROM tsindex.com BELOW</p>
      <form>
          <input id="ts_id_js_html" type="text" value="128856"></input>
          <br>
          <br>
          <button id="execButton">Save/Load</button>
      </form> 
    <div id="initialize">
      <div id="initialize_id"></div>
      <div id="initialize_if"></div>
      <div id="initialize_else"></div>
    </div>

<script src="initialize_id.js"></script>
<script src="options.js"></script>
<script src="load_id.js"></script>
  </body>
</html>

options.js

//Javascript Document
document.getElementById("execButton").addEventListener("click", store); //adds button functionality to button (id=execButton)

    function store(){
    var input_id = document.getElementById("ts_id_js_html").value;

    chrome.storage.local.set({"ts_id_js": input_id}, function (obj) {
    chrome.storage.local.get("ts_id_js", function (obj) {
        console.log("ts_id_js INSIDE func. test (is this your ID?):" + obj.ts_id_js);
   });

});
}

load_id_to_popup.js似乎是这里的问题,这无法获取所需的值。

//Javascript Document
chrome.storage.local.get("ts_id_js", function (obj) {

if (obj.ts_id_js == undefined || null){
    document.getElementById('input_id').dataset.serverid = 128856;
    console.log("Default ID (128856) loaded to popup.html.");
} else {
    document.getElementById('input_id').dataset.serverid = obj.ts_id_js;
    console.log("Stored ID loaded to popup.html.");
}

});

popup.html

<!DOCTYPE html>
<html>
  <head>
    <script src="jquery-2.2.0.min.js"></script> 
  </head>
  <body>
    <div class="ts3index-viewer" id="input_id" data-serverid="128856" data-style="clientc=030B80"><a href="http://ts3index.com/?page=server&id=">TS3index.com</a></div>
    <script src="load_id_to_popup.js"></script>
    <script src="popup.js"></script>
  </body>
</html>

ma​​nifest.json

{
  "manifest_version": 3,

  "name": "Teamspeak - xxxxx Community",
  "description": "This extension will show Users that are Online on our Teamspeaks Server.",
  "version": "1.4.1",
  "options_page": "options.html",

  "browser_action": {
   "default_icon": "ts3.png",
   "default_popup": "popup.html"
  },
  "permissions": [
   "activeTab",
   "storage"
  ],

  "background": {
    "scripts":["popup.js", "load_id.js", "onclick_button.js", "load_id_to_popup.js", "initialize_id.js", "jquery-2.2.0.min.js", "options.js"]
  },

  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": ["popup.js", "load_id.js", "onclick_button.js", "load_id_to_popup.js", "initialize_id.js", "options.js", "jquery-2.2.0.min.js"]
    }
  ]
}

【问题讨论】:

  • backgroundcontent_scripts" field in manifest.json中删除load_id_to_popup.js,再试一次。
  • @HibaraAi 感谢您的想法 - 但这什么也没做 :( 还有其他想法吗?
  • 首先你应该知道你的问题不是chrome.storage.get是如何使用的,而是Cannot read property 'dataset' of null,也就是说document.getElementById('input_id')null,你应该先检查元素是否被检索到。
  • @HibaraAi 但是这个chrome.storage.local.get("ts_id_js", function (obj) {...} 应该得到它,不是吗?我已经使用 options.js 预先设置了 id。我想我没有掌握一些基本的东西。
  • 不要将 jQuery 和所有这些脚本加载到 &lt;all_urls&gt; 中,除非您必须这样做。这是很多代码,每一页都需要负担(我们这些打开了数百个标签的人怎么办?)。您可能真的需要将所有内容加载到每个页面中。然而,即使你的 UI 交互是从页面内部开始的(使用 manifest.json 内容脚本的一个原因),尝试构建扩展,以便在用户开始与你的 UI,然后注入其余部分。

标签: javascript jquery html google-chrome google-chrome-extension


【解决方案1】:

正如灰原爱在 cmets 中所说,实际错误是 document.getElementById('input_id') 返回 null 表示该元素不存在。发生这种情况是因为除了将它们加载到 popup.html 中之外,您还不加选择地将所有弹出脚本加载为 backgroundcontent_scripts。因此,由于不存在匹配的元素,当脚本作为后台脚本(一次)或内容脚本(每个页面)运行时,您会收到报告的错误。

在您的 manifest.json 中,您需要更改在 background 内的 scripts 键和 content_scripts 内的 js 键中定义的脚本。如果没有关于您的扩展的更完整信息,就无法准确确定哪些脚本应该在background 中列出,哪些应该在content_scripts 中加载。但是,除非您知道应该将脚本加载到 backgroundcontent_scripts 中,否则大多数只会加载其中一个。

manifest.json 中的 backgroundcontent_scripts 条目需要如下所示。从backgroundcontent_scripts 条目中,我删除了options.js 的条目,这显然是为您的options.html 准备的。我还删除了 load_id_to_popup.js,这显然是为您的 popup.html 准备的。另外,我删除了 popup.js。虽然您没有在问题中包含它,但它很可能仅适用于您的 popup.html

backgroundcontent_scripts 中,我还注释掉了load_id.jsinitialize_id.js,因为它们看起来适合您的选项。 html。您没有在问题中包含它们,所以我假设它们没有在其他地方真正使用过。

您没有在问题中包含 onclick_button.js,因此不清楚应该在您的 *manifest.json 中的何处(如果有的话)引用它。但是,如果它应该在 manifest.json 中,那么它可能应该只在 backgroundcontent_scripts 中的一个中。

{
  "manifest_version": 2,

  "name": "Teamspeak - xxxxx Community",
  "description": "This extension will show Users that are Online on our Teamspeaks Server.",
  "version": "1.4.1",
  "options_page": "options.html",

  "browser_action": {
   "default_icon": "ts3.png",
   "default_popup": "popup.html"
  },
  "permissions": [
   "activeTab",
   "storage"
  ],

  "background": {
    "scripts":[
      "jquery-2.2.0.min.js", //jQuery needs to be loaded prior to where you use it
      //"load_id.js",        //Appears to be for options.html; probably don't want it here
      "onclick_button.js"  //Unknown; Likely use in only one of background/content_scripts
      //"initialize_id.js",  //Appears to be for options.html; probably don't want it here
    ]
  },

  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        //Try to avoid loading jQuery into <all_urls> unless you really **need** it.
        "jquery-2.2.0.min.js",//jQuery needs to be loaded prior to where you use it
        //"load_id.js",      //Appears to be for options.html; probably don't want it here
        "onclick_button.js"//Unknown;Likely use in only one of background/content_scripts
        //"initialize_id.js",//Appears to be for options.html; probably don't want it here
      ]
    }
  ]
}

此外,您的manifest_version 应该是 2,而不是 3。

【讨论】:

  • 我不知道应该在哪里添加脚本,什么时候不应该添加它们?
  • @frizzant,我在回答中提供了有关我对您的 manifest.json 所做更改的更多信息。通常,脚本只在一处引用(backgroundcontent_scriptspopup.htmloptions.html 之一)。如果您在其中不止一个中引用相同的脚本,则可能有问题。您可以在多个脚本中使用相同的脚本,但这样做并不常见。如果您共享一些通用代码、库、一些全局变量,通常会这样做。这样做通常是有意识的选择。
  • @frizzant,我建议你阅读Chrome extension architecture overview(也许通过阅读从那里链接的页面来工作)。它包含整体架构信息,有助于您了解事物的一般组织/完成方式。
猜你喜欢
  • 2019-06-22
  • 2020-01-29
  • 2021-12-29
  • 1970-01-01
  • 2017-03-12
  • 2021-12-07
  • 2021-11-21
  • 2021-06-07
  • 2022-01-12
相关资源
最近更新 更多