代码之家  ›  专栏  ›  技术社区  ›  Moustafa

Chrome扩展不响应javascript[重复]

  •  -2
  • Moustafa  · 技术社区  · 8 年前

    我正在使用javascript构建一个chrome扩展,标签在浏览器中正常工作,同时添加相同的html,javascript代码到chrome的扩展没有响应。 Json文件:

    {
    
    "name": "SOB",
    "version": "1.0",
    "manifest_version":2,
    "permissions": ["storage",
                    "activeTab" ],
    "icons" : {
                "16" : "16.png" ,
                "48" : "48.png"
                },
    
    "browser_action": {
        "default_icon": "Skype Orange.png",
        "default_popup": "popup.html"
    },
    
    "background": {
        "scripts": ["background.js"],
        "persistent": true
      }
    
    
    
    
    }
    

    Popup.html:

        <!doctype html>
    
        <html>
          <head>
            <title>Getting Started Extension's Popup</title>
            <style>
              body {
                font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
                font-size: 100%;
              }
    
            </style>
    
    
    
            <script src="background" type="text/javascript"></script>
          </head>
          <body>
    
    
    
        <input type="text" name="value"   id="fillIn"  /> 
    <input type="submit" value="Submit"  onClick="response()"/>
    <p id="answer"></p>
    
        <input type="text" name="value" id="input"  placeholder="SOB Check" />
        <button id="form"   onClick="demo()"> Submit</button>
        <p id="output"> </p>
    
        <!----------
            <div id="status"></div>
            <img id="image-result" hidden>
        ------------>
         </body>
        </html>
    

    background.js:

    function demo()
    {
    var arra = [] , i = 1, rem ;
    
    var Input =document.getElementById('input').value;
    var x = 1;
    while(Input > 0)
    {
         rem = Input%2;
        Input = (parseInt(Input/ 2));
        arra[i] = rem ;
        i++;
    
    }
    
    while ( x < 35 )
    {
        if(arra[x]  == 1)
        {
            output.innerHTML +=("SOB "+ x +" Found" +"<br />" );
            x = x +1 ;
            }
        else
        {
                x = x +1 ;
                }
    
    
    }
    }
    function response() {
    
    var box = document.getElementById("fillIn");
    
    switch (box.value)
    {
    case '0' : document.getElementById("answer").innerHTML="Successful";
               break ;
    case '999' : document.getElementById("answer").innerHTML="Other Error No Retry";
    break ;
    }
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Lakshay Dulani    8 年前

    您需要在上运行代码 DOMContentLoaded 事件

    document.addEventListener('DOMContentLoaded', function () {
      demo();
    });
    

    你提到 background 而不是 background.js 在弹出的html文件中。