Skip to content

Instantly share code, notes, and snippets.

@VDK
Created December 1, 2023 14:56
Show Gist options
  • Save VDK/a45aea7ddd12c8bc8edf46e3f6b6fd78 to your computer and use it in GitHub Desktop.
Save VDK/a45aea7ddd12c8bc8edf46e3f6b6fd78 to your computer and use it in GitHub Desktop.
Delpher.user.js
// ==UserScript==
// @name Delpher met Wikitext
// @supportURL https://nl.wikipedia.org/wiki/Gebruiker:1Veertje/Delpher_userscript
// @namespace https://*.delpher.nl/nl/*
// @version 0.8
// @description Delpher WP improvement
// @author 1Veertje
// @match https://*.delpher.nl/nl/*
// @grant none
// ==/UserScript==
$( document ).ready(function() {
const today = new Date().toISOString().slice(0,10);
const data = JSON.parse( $($("div.js-object-viewer-wrapper")[0]).attr('data-metadata'));
const label = $( "label" )
.map((i, el) => el.innerText.trim()).get()
.filter(e => e !== 'Pagina')
.reverse()[0].toLowerCase();
const meta = [];
meta.title = data.title;
meta.work = data.alternative;
meta.pubdate = new Date(data.subtitle.trim().replace(/(\d+)-(\d+)-(\d+)/,'$3-$2-$1')).toISOString().slice(0,data.subtitle.trim().length);
meta.url = `https://resolver.kb.nl/resolve?urn=${data.objectlevelId}`;
const citeArticle = buildRef(meta, today)
meta.url = $( ".js-share-input-i3" )[0].value;
meta.pagenr = meta.url.replace(/.+?0*(\d+)$/,'$1');
meta.title = (meta.pagenr == "1" ? "Voorpagina " : "") + meta.work + (meta.pagenr != "1" ? ' p. '+ meta.pagenr : "")
const citePage = buildRef(meta, today)
insertLabels("Ref pagina", citePage);
insertLabels(`Ref ${label}`, citeArticle);
});
function buildRef(meta, today){
return `<ref>{{Cite web
| title = ${htmlEntities(meta.title)}
| date = ${meta.pubdate}
| url = ${meta.url}
| work = ${meta.title.indexOf(meta.work) == 0 ? "": meta.work}
| via = Delpher
| accessdate = ${today}
}}</ref>`;
}
function htmlEntities(str) {
//credit: https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
function insertLabels(label, wikitext){
//mobile share
const htmlRefPage1 = $('<div/>').html( label_html_mobile(label, wikitext));
$(htmlRefPage1[0]).insertBefore($($('dl')[0]));
//normal share
const htmlRefPage2 = $('<div/>').html( label_html(label, wikitext));
$(htmlRefPage2[0]).insertBefore($($('div.object-view-menu__share-links-details')[0]));
}
function label_html(label,input){
const identifier = Math.random().toString(36).substring(7);
return `<div class="object-view-menu__share-links-details hidden-input" style="display: flex;">
<label for="${identifier}" class="object-view-menu__share-links-details-label">${label}</label>
<div id="${identifier}" class="object-view-menu__share-links-details-link">
<input class="object-view-menu__share-links-details-input input-field" type="text" onclick="this.setSelectionRange(0, this.value.length)" onchange="$(this).val(\'${input}\')" value="${input}" readonly="">
</div>
</div>`;
}
function label_html_mobile(label, input){
return `<label class="metadata__details-label">${label}</label>
<div class="metadata__details-input-wrapper">
<input type="text" class="js-share-input-i5 persistent-id input-field metadata__details-input" onclick="this.setSelectionRange(0, this.value.length)" onchange="$(this).val(\'${input}\')" value="${input}" readonly="">
</div>`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment