I’m developing a Chrome/Edge extension that captures selected text in PDF files and logs it to the console. The script works fine on regular web pages, but it doesn’t capture text in the PDF viewer in Edge.
Here is my manifest.json:
{
"manifest_version": 3,
"name": "PDF Text Selection Logger",
"version": "1.0",
"permissions": [
"activeTab",
"scripting",
"file:///*"
],
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_end"
}
]
}
My background.js to inject the content script:
console.log("Service worker starting...");
chrome.action.onClicked.addListener((tab) => {
console.log("Action clicked, executing content script...");
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["content.js"]
}, () => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
} else {
console.log("Content script executed successfully.");
}
});
});
console.log("Service worker started.");
And the content.js script:
(function() {
'use strict';
function getSelectedText() {
let selectedText = '';
if (window.getSelection) {
selectedText = window.getSelection().toString();
}
return selectedText;
}
function handleSelection() {
const selectedText = getSelectedText();
if (selectedText) {
console.log('Selected text: ', selectedText);
}
}
document.addEventListener('mouseup', handleSelection);
document.addEventListener('keyup', handleSelection);
console.log("PDF Text Selection Logger script activated.");
})();
Does anyone know how to correctly inject a content script into the Edge PDF viewer to capture selected text? Any help would be appreciated!