User:Shisma/userscript-wgarticleId.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// ==UserScript==
// @name         Update URL with wgArticleId
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Extract wgArticleId from script and update URL accordingly
// @author       User:Shisma
// @match        https://simpsonswiki.com/wiki/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to extract wgArticleId and update URL
    const updateURLWithArticleId = () => {
        // Get all script elements
        const scripts = document.getElementsByTagName('script');
        for (const script of scripts) {
            // Using regex to find the wgArticleId from script contents
            const match = /"wgArticleId":(\d+)/.exec(script.textContent);
            if (match && match[1]) {
                const articleId = match[1];
                const newUrl = `${window.location.origin}/w/index.php?curid=${articleId}`;
                // Update the URL without creating a new history entry
                window.history.replaceState({}, '', newUrl);
                console.log('URL updated to include wgArticleId:', newUrl);
                break;
            }
        }
    };

    // Run the function when the document is loaded
    updateURLWithArticleId();
})();