【问题标题】:Chrome ext:appendChildTypeError: Cannot call method 'appendChild' of null when trying to build overlay divChrome ext:appendChildTypeError:尝试构建覆盖 div 时无法调用 null 方法“appendChild”
【发布时间】:2012-11-27 10:05:34
【问题描述】:

我尝试构建来自 chrome 扩展的简单覆盖 div,它大部分时间都在工作 但我注意到很少有网站像:http://en.wikipedia.org/wiki/Main_Page 尝试添加 div 时出现此错误。

这是我的代码: 内容.js:

var s = document.createElement('script');
s.src = chrome.extension.getURL('script_inpage.js');
s.onload = function() {
    s.parentNode.removeChild(s);
};

try{
 console.log("appendChild START");
    (document.head||document.documentElement||document.body).appendChild(s);
 console.log("appendChild DONE");
}catch(e){
  console.log("exception in appendChild");
}  

script_inpage.js

try{
    var buttonnode= document.createElement('input');
    buttonnode.setAttribute('type','button');
    buttonnode.setAttribute('id','test_btn');
    buttonnode.setAttribute('value','Test!');


    var div_bottom = document.createElement("div");
    div_bottom.id = "div_bottom";
    div_bottom.style.width = "300px";
    div_bottom.style.height = "20px";
    div_bottom.style.background = "#999";
    div_bottom.style.position="fixed";
    div_bottom.style.bottom="0";
    div_bottom.style.right="0";
    div_bottom.style.zIndex="9999"
    div_bottom.innerHTML = "Hello from div";
    div_bottom.appendChild(buttonnode);
    document.body.appendChild(div_bottom);
}
catch(e){
  console.log("script in page exception in appendChild"+e);
  alert(e);
} 

manifist.json

{
   "background": {
      "page": "background.html" 
   },
   "content_scripts": [
    {
      "matches": ["<all_urls>"],       
      "js": ["contentscript.js"],
      "run_at": "document_end",
      "all_frames": true
    }
   ],
   "web_accessible_resources": [
    "script_inpage.js"
   ],
   "browser_action": {
      "default_icon": "icon19.png",
      "default_popup": "popup.html",
      "default_title": "Simple test"
   },
   "content_security_policy": "script-src 'self'; media-src *; object-src 'self'",
   "description": "Simple test.",
   "icons": {
      "128": "icon128.png",
      "16": "icon16.png",
      "32": "icon32.png",
      "48": "icon48.png"
   },

   "manifest_version": 2,
   "minimum_chrome_version": "20",
   "name": "Simple test",
   "permissions": [ 
        "unlimitedStorage",
        "http://*/",
        "<all_urls>",
        "tabs"
   ],

   "version": "2.6"
}

【问题讨论】:

    标签: javascript exception google-chrome-extension


    【解决方案1】:

    http://en.wikipedia.org/wiki/Main_Page 使用以下代码对我正常工作

    manifest.json

    {
    
       "content_scripts": [
        {
          "matches": ["<all_urls>"],       
          "js": ["content.js"],
          "run_at": "document_end",
          "all_frames": true
        }
       ],
       "web_accessible_resources": [
        "scripts.js"
       ],
       "browser_action": {
          "default_title": "Simple test"
       },
       "content_security_policy": "script-src 'self'; media-src *; object-src 'self'",
       "description": "Simple test.",
    
    
       "manifest_version": 2,
       "minimum_chrome_version": "20",
       "name": "Simple test",
       "permissions": [ 
            "unlimitedStorage",
            "http://*/",
            "<all_urls>",
            "tabs"
       ],
    
       "version": "2.6"
    }
    

    scripts.js

    try{
        var buttonnode= document.createElement('input');
        buttonnode.setAttribute('type','button');
        buttonnode.setAttribute('id','test_btn');
        buttonnode.setAttribute('value','Test!');
    
    
        var div_bottom = document.createElement("div");
        div_bottom.id = "div_bottom";
        div_bottom.style.width = "300px";
        div_bottom.style.height = "20px";
        div_bottom.style.background = "#999";
        div_bottom.style.position="fixed";
        div_bottom.style.bottom="0";
        div_bottom.style.right="0";
        div_bottom.style.zIndex="9999"
        div_bottom.innerHTML = "Hello from div";
        div_bottom.appendChild(buttonnode);
        document.body.appendChild(div_bottom);
    }
    catch(e){
      console.log("script in page exception in appendChild"+e);
      alert(e);
    } 
    

    content.js

    var s = document.createElement('script');
    s.src = chrome.extension.getURL('scripts.js');
    s.onload = function() {
        s.parentNode.removeChild(s);
    };
    
    try{
     console.log("appendChild START");
        (document.head||document.documentElement||document.body).appendChild(s);
     console.log("appendChild DONE");
    }catch(e){
      console.log("exception in appendChild");
    } 
    

    【讨论】:

    • @user63898 :刚刚从 manifest.html 中删除了 "background": { "page": "background.html" }, "default_popup": "popup.html",,因为您没有为它们共享代码;它起作用了,这意味着这些脚本中存在问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多