gpt4 book ai didi

javascript - 单击事件在 Chrome 扩展程序的后台页面中不起作用

转载 作者:行者123 更新时间:2023-11-28 00:01:46 28 4
gpt4 key购买 nike

我添加了一个背景页面,其中包含一个带有按钮的表单。我正在使用 jQuery 来处理 DOM 和事件。在点击 Search 按钮时它没有按预期工作,它应该显示 alert( "Handler for .click() called.");

更新:在控制台上我得到:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".

相关代码如下:

list .json

{
"manifest_version": 2,
"name" : "my Ext,
"description" : "Copy Rows",
"version" : "1.0",
"content_scripts" : [{
"matches" : ["http://example.com/"],
"js" : ["jquery.js","script.js"]
}],
"background":{
"scripts": ["jquery.js","background.js"]
},
"browser_action": {
"default_title": "Title"
}
}

script.js

var currentURL = document.location.href;
$(document).ready(function(){

$("#button").click(function()
{
alert( "Handler for .click() called." );
});
});

chrome.extension.onMessage.addListener(function(msg, sender, sendResponse)
{
alert(msg.param);
if (msg.method == "getHTML")
sendResponse({data: "Welcome Bg"});
});

background.js

chrome.browserAction.onClicked.addListener(function(activeTab){
var loaderURL = chrome.extension.getURL("dashboard.html");
chrome.tabs.create({ url: loaderURL });
});

function search()
{
// alert("hi search");
// chrome.tabs.sendMessage(activeTab.id, {method: "getHTML",param:"myParam"}, function(response) {
// alert(response.data);
// });
}
$(document).ready(function()
{
$("#button").click(function()
{
alert( "Handler for .click() called." );
});

});

dashboard.html(我的后台页面)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="jquery.js"></script>
</head>
<body>
<form>
<textarea id="serch"></textarea>
<input id="button" type="button" value="Search" />
</form>
</body>
</html>

最佳答案

问题在这里解决了(基本上内联脚本是不允许的)

chrome extension insert content script on browser action

关于javascript - 单击事件在 Chrome 扩展程序的后台页面中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916593/

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