【发布时间】:2022-01-10 18:32:09
【问题描述】:
我正在开发一个 Google Chrome 扩展程序,它具有突出显示特定单词的功能。我已经能够获得扩展以突出显示 popup 窗口中的单词。但是,当我尝试修改代码并突出显示 Active Tab 中的单词时,我遇到了问题。我正在尝试使用 chrome.tabs.sendMessage 来执行操作。该消息永远不会到达我现有的listener。也许我把它放在错误的文件中?
popup.html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="button.css">
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-3.6.0.min.js"></script>
<script src="popup.js"></script>
<script src="selection.js"></script>
<script src="hilitor.js"></script>
</head>
<body>
<button id="highlightText">Scan for Washington</button>
</body>
</html>
background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.message == "getSelection"){
console.log("Inside Get Selection");
var myHilitor = new Hilitor("content"); // id of the element to parse
myHilitor.apply("washington state one");
console.log(request, sender, sendResponse);
}
sendResponse();
});
popup.js
$(function(){
$('#highlightText').click(function(){highlightAllText();});
});
function highlightAllText() {
console.log("Inside Highlight All Text");
chrome.windows.getCurrent(w => {
chrome.tabs.query({active: true, windowId: w.id}, tabs => {
chrome.tabs.sendMessage(tabs[0].id, {
"message": "getSelection"
});
});
});
我目前使用Washington Wikipedia 作为我的页面来突出显示。我现在也在使用Hilitor Library 来执行突出显示。同样,我可以突出显示从 popup.html 创建的 popup 窗口中的所有内容,但我无法突出显示之外的内容。
【问题讨论】:
标签: javascript highlight sendmessage chrome-extension-manifest-v3