Skip to content

Instantly share code, notes, and snippets.

@vyznev
Last active March 31, 2021 22:08
Show Gist options
  • Save vyznev/b078af6b3574f80879f93675f59d8c86 to your computer and use it in GitHub Desktop.
Save vyznev/b078af6b3574f80879f93675f59d8c86 to your computer and use it in GitHub Desktop.
The ETCSL website sometimes loses track of the selected encoding. This script fixes it and makes switching between Unicode and ASCII easy.
// ==UserScript==
// @name ETCSL charenc fix
// @namespace https://github.com/vyznev/
// @description The ETCSL website sometimes loses track of the selected encoding. This script fixes it and makes switching between Unicode and ASCII easy.
// @author Ilmari Karonen
// @version 1.1
// @match *://etcsl.orinst.ox.ac.uk/*
// @homepageURL https://gist.github.com/vyznev/b078af6b3574f80879f93675f59d8c86
// @downloadURL https://gist.github.com/vyznev/b078af6b3574f80879f93675f59d8c86/raw/etcsl_charenc_fix.user.js
// @run-at document-start
// @grant none
// ==/UserScript==
(function () {
const scriptURL = new URL('https://etcsl.orinst.ox.ac.uk/cgi-bin/etcsl.cgi');
// If the URL has no explicit charenc parameter, use charenc=gcirc (Unicode)
const locURL = new URL(location);
if (locURL.pathname === scriptURL.pathname && locURL.search && !locURL.searchParams.get('charenc')) {
locURL.searchParams.set('charenc', 'gcirc');
console.log(`ETCSL charenc fix rewriting location ${location} to ${locURL}`)
return location.replace(locURL);
}
function domFixes () {
// If it does, but some links on the page don't, fix them
const charenc = locURL.searchParams.get('charenc') || 'gcirc';
const links = document.querySelectorAll('a[href]');
for (const link of links) {
const url = new URL(link.href, location);
if (url.host === scriptURL.host && url.pathname === scriptURL.pathname && !url.searchParams.get('charenc')) {
url.searchParams.set('charenc', charenc);
console.log(`ETCSL charenc fix rewriting link ${link.href} to ${url}`)
link.href = url;
}
}
// Inject links for switching between Unicode and ASCII encodings
const subtitle = document.querySelector('body > hr + h2');
if (subtitle && locURL.pathname === scriptURL.pathname) {
function makeLink(enc, label) {
const url = new URL(location);
let link = document.createElement('b');
if (url.searchParams.get('charenc') !== enc) {
url.searchParams.set('charenc', enc);
link = document.createElement('a');
link.href = url;
}
link.append(label);
return link;
}
const wrapper = document.createElement('div');
wrapper.append('Select encoding: ');
wrapper.append(makeLink('gcirc', 'Unicode'));
wrapper.append(' | ');
wrapper.append(makeLink('j', 'Ascii'));
wrapper.style = 'float: right';
console.log(`ETCSL charenc fix injecting`, wrapper);
subtitle.parentNode.insertBefore(wrapper, subtitle);
}
}
if (document.readyState !== 'loading') {
domFixes();
} else {
window.addEventListener('DOMContentLoaded', domFixes);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment