Last active
January 23, 2023 01:07
-
-
Save thundergnat/c5a86a6d5e0018ac67bdea3fc48786a0 to your computer and use it in GitHub Desktop.
Tampermonkey / Greasemonkey script to add Language link parameters to Rosettacode Language Category pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Language links | |
// @description Adds language parameters to category links on Rosettacode.org | |
// @author thundergnat | |
// @namespace mailto:thundergnat@gmail.com | |
// @version 0.8 | |
// @updateURL https://gist.github.com/thundergnat/c5a86a6d5e0018ac67bdea3fc48786a0#file-language_links-user-js | |
// @downloadURL https://gist.github.com/thundergnat/c5a86a6d5e0018ac67bdea3fc48786a0#file-language_links-user-js | |
// @match *://rosettacode.org/wiki/Category* | |
// @icon https://www.google.com/s2/favicons?domain=www.rosettacode.org | |
// @grant none | |
// @run-at document-end | |
// ==/UserScript== | |
// Javascript applet to add language link parameters to Language Category pages | |
// on Rosettacode.org. By default, task entry links on the Language page just | |
// point to the top of the task page. This script will add a language link parameter | |
// to have the link point directly to the specific task entry referred to. No more | |
// need to scroll half a mile when you follow a link. | |
// | |
// Beings I don't know Javascript, and hacked this together with the help of Google, | |
// W3schools, and stackoverflow, this may be full of bugs and inefficiencies. If you | |
// spot any, let me know. | |
// -- thundergnat | |
// Check to see if the url already has a language link. | |
function hasLanguageLink(url){ return !!url.match(/#/); } | |
function addLanguageLinks(Links) { | |
for (var i = 0; i < Links.length; i++) { // iterate through the links. | |
if(!hasLanguageLink(Links[i].href)) { // if it doesn't already have a language link | |
Links[i].setAttribute('href', Links[i].href + '#' + language); // modify the href. | |
} | |
} | |
} | |
// Check to see if the page has an infobox. Pretty much only language category | |
// pages have one. Avoid modifying other category pages (Programming_tasks, | |
// Language_users, implementations, examples_needing_attention, Programming_languages, etc). | |
// If it isn't a Language page, halt further unnecessary processing. | |
if (!!document.querySelector("div.infobox")) { | |
// Locate the page title header | |
var header = document.querySelector("span.mw-page-title-main"); | |
// Grab the page name from the header - nominally the language name, | |
// actually the Category name. Massage it a bit into the correct format. | |
var language = encodeURIComponent(header.innerHTML.replace(/ /g, "_")).replace(/\%/g, "."); | |
// Fix up a few other characters that are legal in uris but Rosettacode doesn't like. | |
language = language.replace(/\*/g, ".2A").replace(/\!/g, ".21"); | |
// Compensate for F# and C# and languages with initial lowercase letter manually. | |
var specialCase = { | |
"Bc": "bc", | |
"C_sharp": "C.23", | |
"Dc": "dc", | |
"EC": "eC", | |
"ElastiC": "elastiC", | |
"F_Sharp": "F.23", | |
"Ferite": "ferite", | |
"Friendly_interactive_shell": "friendly_interactive_shell", | |
"Gecho": "gecho", | |
"Hexiscript": "hexiscript", | |
"I": "i", | |
"Jq": "jq", | |
"Min": "min", | |
"MIRC_Scripting_Language": "mIRC_Scripting_Language", | |
"NewLISP": "newLISP", | |
"OoRexx": "ooRexx", | |
"Sed": "sed", | |
"Smart_BASIC": "smart_BASIC", | |
"Zkl": "zkl", | |
"Icon": "Icon_and_Unicon", | |
"Unicon": "Icon_and_Unicon" | |
}; | |
if (language in specialCase) { language = specialCase[language] } | |
// Find all of the 'mw-category' divs on the page. Usually there's only one, but | |
// languages with 'See also' sections on their Category page have multiple. | |
// Lookin' at you Phix and Python... | |
var containers = document.querySelectorAll("div.mw-category"); | |
// Languages with very few examples won't have the 'mw-category' div, use an alternate | |
// that all seem to have, but is too broad for languages with more than about 10 examples. | |
if(containers.length == 0) { | |
containers = document.querySelectorAll("div.mw-content-ltr"); | |
} | |
// Program entries should be in the last container div on the page. | |
var container = containers[containers.length - 1]; | |
// Check to see if container is actually Subcategories div (no task entries) and bail if it is. | |
if (container.querySelector("H3").innerHTML === '@') { return } | |
// Gather all the relevant links from the container. | |
var links = container.querySelectorAll("a"); | |
window.onload = addLanguageLinks(links); // Add links after the page has loaded. | |
setInterval(addLanguageLinks(links), 1000); // Periodically check to see if they need to be updated. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version 0.8 - 2023-01-22
Version 0.7 - 2021-06-16
Version 0.6:
Version 0.5:
Version 0.4:
Version 0.3:
Version 0.2:
Version 0.1: