我創建了一個擴展,將腳本放入頁面標記并根據以下
清單.json
{
"name": "content_scripts popup",
"version": "1.0",
"manifest_version": 3,
"content_scripts": [
{
"js": [
"matches.js"
],
"matches": [
"<all_urls>"
]
}
],
"host_permissions": [
"<all_urls>"
],
"action": {
"default_popup": "popup.html"
}
}
popup.js
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
console.log("Send");
chrome.tabs.sendMessage(tabs[0].id, "message", (response) => {
console.log("Recv response = " response.title);
document.getElementById("title").innerText = response.title;
});
});
匹配.js
console.log("matches.js");
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Recv. Send response = " document.title);
sendResponse({ title: document.title });
return true;
});
popup.html
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* {
font-size: x-large;
}
</style>
</head>
<body style="min-width:300px">
<div id="title"></div><br>
<script src="popup.js"></script>
</body>
</html>
uj5u.com熱心網友回復:
我不確定你到底在問什么,但我假設你有 3 件事:(1) 彈出視窗,(2) 內容腳本,以及 (3) 注入頁面的腳本,在頁面背景關系中運行. 首先,要將訊息發送到彈出視窗,您可以運行chrome.runtime.sendMessage()并在彈出視窗的 script.js 檔案中使用chrome.runtime.onMessage(). 從頁面腳本中獲取資料是不同的,它不能直接與您的瀏覽器擴展通信。因此,從您的頁面背景關系腳本中,您可以創建一個具有唯一 ID 的元素并將一些資料存盤在一個屬性中。使用 MutationObserver 的修改來運行 waitForElm() 函式(請參閱此執行緒 -如何等待元素存在?)。在您的內容腳本中,您將等待創建元素,從您設定的屬性中收集資料,然后運行chrome.runtime.sendMessage()如前所述,將這些資料獲取到您的彈出視窗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/530643.html
標籤:javascript谷歌浏览器谷歌浏览器扩展chrome 扩展清单-v3
下一篇:為什么這個按鈕不能有邊框半徑?
