I’m trying to run a Tampermonkey script that captures the selected text and logs it to the console. The script works fine on regular web pages but doesn’t seem to execute on PDF pages in the Edge browser.
Here is the code I’m using:
// ==UserScript==
// @name PDF Text Selection Logger
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Log selected text to the console in PDFs
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to capture selected text
function getSelectedText() {
let selectedText = '';
if (window.getSelection) {
selectedText = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
selectedText = document.selection.createRange().text;
}
return selectedText;
}
// Add mouseup event to capture the selection
document.addEventListener('mouseup', () => {
const selectedText = getSelectedText();
if (selectedText) {
console.log('Selected text: ', selectedText);
}
});
// Add keyup event to capture the selection when a key is released
document.addEventListener('keyup', () => {
const selectedText = getSelectedText();
if (selectedText) {
console.log('Selected text: ', selectedText);
}
});
console.log("Text selection capture script activated.");
})();
This script successfully logs the selected text on normal web pages, but it doesn’t run at all on PDF pages viewed in Edge. Is there any specific way to get Tampermonkey scripts to run on PDF pages in the Edge browser? Any help or pointers would be greatly appreciated.