2012-04-11 98 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

相关问题