gpt4 book ai didi

google-chrome-extension - 创建 chrome 扩展后无法访问 popup.js 文件

转载 作者:行者123 更新时间:2023-12-01 11:01:36 26 4
gpt4 key购买 nike

list .json

{
"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;
}

当我加载这个扩展时,我可以正常看到。但是当我填充 deger1 和 deger2 文本框并单击按钮时,功能不起作用,在 sonuc 文本框(结果文本框)中为空。我该如何解决?我是创建 chrome 扩展的新手。感谢您的帮助。

最佳答案

你有 manifest_version : 2在你的 list 中,所以请去阅读它引入的变化....
http://code.google.com/chrome/extensions/manifestVersion.html
你进入了控制台Refused to execute inline event handler because of Content-Security-Policy , 因为你的 html ( <input type="button" value="Hesapla" onclick="hesaplama()" /> ) 中的 onclick 事件处理程序。对于 list 版本 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;
}

关于google-chrome-extension - 创建 chrome 扩展后无法访问 popup.js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10105440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com