我想在点击扩展程序时自动点击网站上的按钮
I want to auto click a button on the website as the extension is clicked
Objective- 当用户点击特定页面上的 chrome 扩展时,它会自动点击该网站页面上的特定按钮。
我尝试了以下方法来生成自动点击,但其中 none 有效。
我从 content-script.js 中获取 background.js 中的 DOM 和运行正在执行以下命令(在后台页面中使用这些命令用于其他目的,在内容脚本中 运行 时出现相同的错误)。
$(html).find(".button").click();
$(html).find('#button').addEventListener('click', function() {
alert("hello"); });
button.addEventListener('click', function() {
alert("hello"); });
它要么给我错误,要么不起作用。
第一个命令错误-
jquery.min.js:4 Refused to execute inline event handler because it
violates the following Content Security Policy directive: "script-src
'self' blob: filesystem: chrome-extension-resource:". Either the
'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce
('nonce-...') is required to enable inline execution.
我也试过在我的manifest.json中添加这个,但错误没有改变-
"content_security_policy": "script-src 'self' 'unsafe-eval';
object-src 'self'"
请帮助我。谢谢
为了自动点击按钮,你可以使用 jquery 的 trigger() 方法,如下所示:
$( "#foo" ).on( "click", function() {
alert( $( this ).text() );});
$( "#foo" ).trigger( "click" );
希望对您有所帮助:)
您可以在后台脚本中尝试此代码:
chrome.browserAction.onclicked.addListener(tab => {
chrome.tabs.executeScript(tab, {code:
`var theButton = document.querySelector("button");
if (theButton) theButton.click();`});
});
当您的扩展图标 (browser action) 被点击时,它会点击活动网页的第一个按钮。如果您想要一个特定的按钮,请在 document.querySelector
调用中优化选择器。
Objective- 当用户点击特定页面上的 chrome 扩展时,它会自动点击该网站页面上的特定按钮。
我尝试了以下方法来生成自动点击,但其中 none 有效。
我从 content-script.js 中获取 background.js 中的 DOM 和运行正在执行以下命令(在后台页面中使用这些命令用于其他目的,在内容脚本中 运行 时出现相同的错误)。
$(html).find(".button").click();
$(html).find('#button').addEventListener('click', function() { alert("hello"); });
button.addEventListener('click', function() { alert("hello"); });
它要么给我错误,要么不起作用。
第一个命令错误-
jquery.min.js:4 Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
我也试过在我的manifest.json中添加这个,但错误没有改变-
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
请帮助我。谢谢
为了自动点击按钮,你可以使用 jquery 的 trigger() 方法,如下所示:
$( "#foo" ).on( "click", function() {
alert( $( this ).text() );});
$( "#foo" ).trigger( "click" );
希望对您有所帮助:)
您可以在后台脚本中尝试此代码:
chrome.browserAction.onclicked.addListener(tab => {
chrome.tabs.executeScript(tab, {code:
`var theButton = document.querySelector("button");
if (theButton) theButton.click();`});
});
当您的扩展图标 (browser action) 被点击时,它会点击活动网页的第一个按钮。如果您想要一个特定的按钮,请在 document.querySelector
调用中优化选择器。