I’m trying to make an extension that copies the selected text on a web page and sends it to an extension popup. I followed this but its not working, its throwing these errors instead:
Here’s my files and code:
manifest.json
"manifest_version": 3,
"name": "Test",
"description": "...",
"version": "0.1",
"action": {
"default_popup": "popup.html",
"default_icon": "icon.png",
"default_title": "Test"
},
"permissions": [
"tabs"
],
"host_permissions": [
"<all_urls>"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["scripts/selection.js"],
"run_at": "document_start",
"all_frames": true
}
]
}
scripts/popup.js
$(function(){
$('#paste').click(function(){
pasteSelection();
});
});
function pasteSelection() {
chrome.tabs.query({
active:true, windowId: chrome.windows.WINDOW_ID_CURRENT
}, function(tab) {
chrome.tabs.sendMessage(tab[0].id, {method: "getSelection"},
function(response){
var text = document.getElementById('text');
text.innerHTML = response.data;
});
});
}
scripts/selection.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "getSelection")
sendResponse({data: window.getSelection().toString()});
else
sendResponse({}); // snub them.
});
I reloaded the extension after I added the code. Opened a wikipedia page, selected some text, and clicked on the extension. and right away the errors came in. I tried clicking the “Paste Selection” button aswell. but it did nothing, it just sent the same errors again.