Adds link for headword and links for sub-entries if there are any.
Headword:
en espagnol | Conjugaison [FR] | Conjugator [EN] | en contexte | images
Wiktionnaire
Sub-entries:
// ==UserScript== | |
// @name WR Wiktionnaire links | |
// @namespace | |
// @version 0.1.1 | |
// @description Add Wiktionnaire links on WordReference French. | |
// @author William Andrea (wjandrea) | |
// @match https://www.wordreference.com/fren/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=wordreference.com | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const my_id_single = 'Wiktionnaire'; | |
const my_id_multiple = 'WiktionnaireMultiple'; | |
const headword_selector = '#articleHead h3.headerWord'; | |
const multiple_selector = '#multipleEntriesContainer a.multipleEntries'; | |
const single_container_id = 'WHlinks'; | |
const multiple_container_id = 'multipleEntriesContainer'; | |
const wk_link = (word) => `https://fr.wiktionary.org/wiki/${word}#fr` | |
const label = 'Wiktionnaire'; | |
function addSingle() { | |
// If already present | |
if (document.getElementById(my_id_single)) return; | |
var elem = document.querySelector(headword_selector); | |
var headword = elem.textContent.trim(); | |
console.debug(`Headword: ${headword}`); | |
var new_div = document.createElement('div'); | |
new_div.setAttribute('id', my_id_single); | |
var link = document.createElement('a'); | |
link.setAttribute('href', wk_link(headword)); | |
new_div.appendChild(link); | |
var text = document.createTextNode(label); | |
link.appendChild(text); | |
console.log("Adding Wiktionnaire link to headword:", new_div); | |
var single_container = document.getElementById(single_container_id); | |
single_container.appendChild(new_div); | |
} | |
function addMultiple() { | |
const sep = ', ' | |
// If already present | |
if (document.getElementById(my_id_multiple)) return; | |
// If only single entry | |
if (!document.getElementById(multiple_container_id)) return; | |
var new_div = document.createElement('div'); | |
new_div.setAttribute('id', my_id_multiple); | |
var div_text = document.createTextNode(`${label} : `); | |
new_div.appendChild(div_text); | |
for (var elem of document.querySelectorAll(multiple_selector)) { | |
var word = elem.textContent.trim(); | |
console.debug(`Word: ${word}`); | |
var link = document.createElement('a'); | |
link.setAttribute('href', wk_link(word)); | |
var text = document.createTextNode(word); | |
link.appendChild(text); | |
new_div.appendChild(link); | |
var sep_text = document.createTextNode(sep); | |
new_div.appendChild(sep_text); | |
} | |
// Delete trailing sep. | |
new_div.removeChild(sep_text); | |
console.log("Adding Wiktionnaire links to subentries:", new_div); | |
var multiple_container = document.getElementById(multiple_container_id); | |
multiple_container.appendChild(new_div); | |
} | |
addSingle(); | |
addMultiple(); | |
})(); |