"worder_check.js" Source Code
// Dieses Javascript öffnet beim Klick auf ein Wort das auf einer HTML Seite steht ein Wörterbuch
document.addEventListener('dblclick', function(event) {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const bereich = selection.getRangeAt(0);
const textfeld = bereich.startContainer;
if (textfeld.nodeType === Node.TEXT_NODE) {
const text = textfeld.textContent;
const offset = bereich.startOffset;
const wort = holedasWortan(text, offset);
if (wort) {
if (window.confirm('Wollen Sie "' + wort + '" nachschlagen?') === true) {
const screenWeite = window.innerWidth;
const screenHoehe = window.innerHeight;
const weite = Math.round(screenWeite * 0.85); // 85% der Bildschirmbreite
const hoehe = Math.round(screenHoehe * 0.85); // 85% der Bildschirmhöhe
window.open('https://de.thefreedictionary.com/' + wort , '_blank', `width=${weite},height=${hoehe} , noopener,noreferrer`);
}
}
}
}
});
function holedasWortan(text, offset) {
const start = text.lastIndexOf(' ', offset - 1) + 1;
const end = text.indexOf(' ', offset);
if (start === -1 && end === -1) return text; // Ein Wort ohne Leerzeichen
let word = text.substring(start, end === -1 ? text.length : end);
// Bereinigen des Wortes, um nur alphabetische Zeichen zu behalten
word = word.replace(/[^a-zA-ZäöüÄÖÜß]/g, '');
return word;
}