diff --git a/oldnewthing/oldnewthing.js b/oldnewthing/oldnewthing.js new file mode 100644 index 0000000..893cfc0 --- /dev/null +++ b/oldnewthing/oldnewthing.js @@ -0,0 +1,51 @@ +// ==UserScript== +// @name The Old New Thing URL Rewriter +// @author Michael Becker +// @version 1 +// @grant none +// @include https://devblogs.microsoft.com/oldnewthing/* +// ==/UserScript== + +var config = { + "patterns": [ + { + "find": "blogs.msdn.microsoft.com", + "replaceWith": "devblogs.microsoft.com" + }, + { + "find": "blogs.msdn.com/b/oldnewthing/archive", + // "redirectTo": "https://web.archive.org/*/http://%1" + redirectTo: function (originalUrl) { + var parts = originalUrl.split('/'); + if (parts.length > 8) { + var datetime = parts[6] + parts[7] + parts[8]; + return "https://web.archive.org/web/" + datetime + "000000/" + originalUrl; + } + return originalUrl; + } + } + ] +} + +window.addEventListener("load", function() +{ + var links = document.getElementsByTagName("A"); + console.log(links.length + " links found"); + for (var i = 0; i < links.length; i++) + { + for (var j = 0; j < config.patterns.length; j++) { + if (links[i].href.indexOf(config.patterns[j].find) !== -1) { + if (typeof (config.patterns[j].replaceWith) === 'string') { + links[i].href = links[i].href.replace(config.patterns[j].find, config.patterns[j].replaceWith); + } + else if (typeof (config.patterns[j].redirectTo) === 'string') { + links[i].href = config.patterns[j].redirectTo.replace('%1', links[i].href); + } + else if (typeof (config.patterns[j].redirectTo) === 'function') { + links[i].href = config.patterns[j].redirectTo(links[i].href); + } + } + } + } + +});