我是 HTML 和 JS 的新手。我正在尝试使用 Chrome 扩展程序将按钮注入现有页面。我想在单击按钮时提交一个搜索表单,但目前它在页面加载时提交表单,然后重复提交。我做错了什么?
这是我的 manifest.json
"browser_action": {
"default_icon": "icon.png",
"content_scripts" : "script.js"
},
"content_scripts": [
{
"matches": ["https://test.com/*"],
"js": ["script.js"],
"run_at": "document_end"
}
这是我的 script.js
var button = document.createElement("button");
var buttonName = document.createTextNode("Search By Tag");
button.appendChild(buttonName);
document.getElementById("nav-section").appendChild(button).onclick=window.location.href='https://test/search?....c';
您需要将处理程序包装成函数:
document.getElementById("nav-section").appendChild(button).onclick=function() { window.location.href='https://test/search?....c' };
因为目前您的代码执行为:
window.location.href='https://test/search?....c';
它会立即重新加载您的页面。onclick
处理程序。当您的页面重新加载时,这不起作用。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句