![](/img/trans.png)
[英]Call JavaScript method in Chrome extension from web page button click
[英]Google extension how to click a button on the web page?
我創建一個擴展文件github.js
jQuery(document).ready(function($) {
console.log('github.js');
// btn btn-primary shelf-cta
var button = $('.btn.btn-primary.shelf-cta');
console.log('button.html() = ', button.html());
button.click();
});
文件popup.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button id="send_messages">Send Messages</button>
<script src="libs/jquery-v3.3.1.js"></script>
<script src="popup.js"></script>
</body>
</html>
文件popup.js
jQuery(document).ready(function($) {
console.log('popup.js');
$('#send_messages').click(function(event) {
console.log('github test');
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.executeScript(
tabs[0].id,
{file: "libs/jquery-v3.3.1.js"},
function(){
chrome.tabs.executeScript(
tabs[0].id,
{file: "github.js"}
);
}
);
});
});
});
文件manifest.json
{
"name": "test the extension",
"version": "1.0",
"description": "Automatically send the message to the opened projects in the browser",
"permissions": [
"tabs",
"activeTab",
"declarativeContent",
"storage",
"<all_urls>"
],
"options_page": "options.html",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"browser_action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
}
},
"icons": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
},
"manifest_version": 2
}
當我轉到github頁面https://github.com/時,有一個按鈕“閱讀指南”,我想單擊它。 我按下擴展名browser_action按鈕,顯示一個彈出窗口。 當我按下按鈕“發送消息”時,什么也沒有發生。 我可以到達按鈕“閱讀指南”並輸出標題,但是button.click(); 不起作用。 如何單擊按鈕或其他鏈接?
因此,jquery中的單擊需要使用此代碼來完成
button.get(0).click();
不像我那樣
button.click();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.