2012-04-11 136 views
3

manifest.json的它不能創建一個Chrome擴展

{ 
    "name": "Summer", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "This is an addition extension", 
    "browser_action": { 
    "default_icon": "icon.png", 
    "default_popup": "popup.html" 
    } 
} 

popup.html

<!doctype html> 
<html> 
    <head> 
    <title>Getting Started Extension's Popup</title> 

    <!-- JavaScript and HTML must be in separate files for security. --> 
    <script src="popup.js"></script> 
    </head> 
    <body> 
     <form name="form"> 
      <div id="sayi1">Sayı 1 : <input type = "text" name="deger1"></div> 
      <div id="sayi2">Sayı 2 : <input type = "text" name="deger2"></div> 
      <div id="sonuc">Sonuç :  <input type = "text" name="cevap"></div> 
      <div id="button"><input type="button" value="Hesapla" onclick="hesaplama()" /></div> 
     </form> 
    </body> 
</html> 

popup.js

function hesaplama() 
{ 
var sayi1 = window.document.form.deger1.value; 
var sayi2 = window.document.form.deger2.value; 
var toplam = parseFloat(sayi1) + parseFloat(sayi2) ; 
window.document.form.cevap.value = toplam; 
} 
後訪問popup.js文件

當我加載這個擴展,我可以正常看到。但是,當我填充deger1和deger2文本框,並單擊按鈕,函數不起作用,在sonuc文本框(結果文本框)爲空。 我該如何解決它?我是創建Chrome擴展的新手。謝謝你的幫助。

+1

'eval'的濫用?使用'parseFloat(說\ u01311)+ parseFloat(說\ u01312)'而不是... – 2012-04-11 12:39:49

+0

它不能再工作 – cethint 2012-04-11 12:46:39

+0

您是否在控制檯中發生任何錯誤? https://code.google.com/chrome/extensions/tut_debugging.html#inspect-popup – abraham 2012-04-11 18:11:16

回答

6

你已經在你的清單得到了manifest_version : 2,所以請去閱讀它介紹了變化....
http://code.google.com/chrome/extensions/manifestVersion.html
您在控制檯Refused to execute inline event handler because of Content-Security-Policy了,因爲在你的HTML的onclick事件處理程序,(<input type="button" value="Hesapla" onclick="hesaplama()" />) 。使用清單版本2這是不允許的,你需要從你的js代碼中附加事件列表器,而不是在html中。
這是你的代碼的工作版本....
popup.html

<!doctype html> 
<html> 
    <head> 
    <title>Getting Started Extension's Popup</title> 

    <!-- JavaScript and HTML must be in separate files for security. --> 
    <script src="popup.js"></script> 
    </head> 
    <body> 
     <form name="form"> 
      <div id="sayi1">Sayı 1 : <input type = "text" name="deger1"></div> 
      <div id="sayi2">Sayı 2 : <input type = "text" name="deger2"></div> 
      <div id="sonuc">Sonuç :  <input type = "text" name="cevap"></div> 
      <div id="button"><input type="button" value="Hesapla" /></div> 
     </form> 
    </body> 
</html> 

popup.js

function hesaplama() 
{ 
var sayı1 = window.document.form.deger1.value; 
var sayı2 = window.document.form.deger2.value; 
var toplam = (parseFloat(sayı1) + parseFloat(sayı2)) ; 
window.document.form.cevap.value = toplam; 
} 
window.onload = function(){ 
    document.querySelector('input[value="Hesapla"]').onclick=hesaplama; 
} 
+1

歡迎您:)噢,您應該爲該輸入添加一個「id」,並改變querySelector來尋找它。 – PAEz 2012-04-12 10:03:29