Chrome插件 Popup 与 Content-Scripts通信
2023-12-22 22:13:41 266
popup.js
document.getElementById("get").addEventListener("click", function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "get_title_describe" }, function (response) {
document.getElementById("content").innerHTML = response;
});
});
});
popup.html
<body>
<h1>Hello Extensions</h1>
<div id="content"></div>
<button id="get">GET</button>
</body>
content-scripts.js
chrome.runtime.onMessage.addListener(function (
request,
sender,
sendResponse
) {
if (request.action === "get_title_describe") {
sendResponse(data_cache);
}
});