gpt4 book ai didi

javascript - JQuery Chrome 扩展程序无法正常工作

转载 作者:行者123 更新时间:2023-11-29 15:17:46 24 4
gpt4 key购买 nike

我的内容脚本有问题。无论如何我都无法让 Jquery 运行!也许你能帮我弄清楚它是什么?

我试图“捕捉”我在字段中写入的值,并在我单击按钮后将其传递以进行处理。我收到“$ is not defined”错误,这可能意味着它无法加载 JQuery。

这是我的 list 、我的 html 和我的代码:

 {
"name": "Test 2017",
"manifest_version": 2,
"version": "0.1",
"description": "TestApp Jquery",
"browser_action": {
"default_popup": "popup.html",
"default_icon": "icon.png"
},
"background": {
"scripts": ["jquery-3.2.1.min.js",
"background.js"
]
},
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"content_scripts":
[
{
"matches": [ "http://*/*", "https://*/*"],
"js":["jquery-3.2.1.min.js",
"contentscript.js"]
}
]
}

Popup.html:

 <html>
<head>
<script src="jquery-3.2.1.min.js"></script>
<script src="popup.js"></script>
<style type="text/css" media="screen">
body { min-width:250px; text-align: center; }
#click-me { font-size: 20px; }
</style>
</head>
<body>
<div>Input: <input id="input">
<input id="click-me" type="button" value="Go!"/>
</body>

弹出.js

 function clickHandler(e) {
chrome.extension.sendMessage({directive: "popup-click"}, function(response)
{
this.close(); // close the popup when the background finishes processing
request
});
}


document.addEventListener('DOMContentLoaded', function () {
document.getElementById('click-me').addEventListener('click', clickHandler);
})

背景.js

 chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
switch (request.directive) {
case "popup-click":
// execute the content script
chrome.tabs.executeScript(null, { // defaults to the current tab
file: "contentscript.js", // script to inject into page and run in sandbox
allFrames: true // This injects script into iframes in the page and doesn't work before 4.0.266.0.
});
sendResponse({}); // sending back empty response to sender
break;
default:
// helps debug when request directive doesn't match
alert("Unmatched request of '" + request + "' from script to
background.js from " + sender);
}
}
);

我尝试运行 Jquery 的 Contentscript.js:

 var abc =  $("#input").val();
console.log(abc);

然后我得到了错误。

这里有什么帮助吗?为什么我的扩展不能加载 JQuery?我已按正确顺序加载所有脚本。

提前致谢!

最佳答案

对于您要实现的目标,无需使用内容脚本。内容脚本用于控制在浏览器中加载的网站的 DOM。您尝试操作的按钮位于弹出窗口中,由 popup.js 控制,而不是 contentscript.js。只需将 jquery 代码移动到 popup.js,它就会按预期工作。

Content Scripts are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM), they can read details of the web pages the browser visits, or make changes to them.

Popup.html(这里没有任何改变,存在 jquery 脚本标签):

 <html>
<head>
<script src="jquery-3.2.1.min.js"></script>
<script src="popup.js"></script>
<style type="text/css" media="screen">
body { min-width:250px; text-align: center; }
#click-me { font-size: 20px; }
</style>
</head>
<body>
<div>Input: <input id="input">
<input id="click-me" type="button" value="Go!"/>
</body>

弹出.js:

     function clickHandler(e) {
chrome.extension.sendMessage({directive: "popup-click"}, function(response)
{
this.close(); // close the popup when the background finishes processing
request
});
}


document.addEventListener('DOMContentLoaded', function () {
document.getElementById('click-me').addEventListener('click', clickHandler);
})

var abc = $("#input").val();
console.log(abc);

关于javascript - JQuery Chrome 扩展程序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47433020/

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