2016-11-14 92 views
-2

我正在使用javascript構建Chrome擴展標記在瀏覽器中正常工作,同時將相同的html,javascript代碼添加到Chrome擴展沒有響應。 JSON文件:Chrome擴展未對javascript做出響應

{ 

"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 ; 
} 
} 
+0

右擊烏爾擴展的圖標,並點擊彈出式檢查,調試 – Lakshay

回答

0

您將需要在 DOMContentLoaded事件運行的代碼

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

您在參考文檔中引用了background而不是background.js

+0

感謝Lakshay但你的意思,我需要添加此功能background.js「document.addEventListener(‘DOMContentLoaded’,函數(){ 演示(); });「 – Moustafa

+0

是的,如果你想運行background.js中的代碼,當用戶點擊分機的圖標打開彈出 – Lakshay

+0

我會建議你從這裏下載相關的示例分機https://developer.chrome.com/extensions/樣本,然後在其中進行更改 – Lakshay