User:Qwerfjkl/scripts/EasyWikiDev.js

// fork of [[User:Ed6767/EasyWikiDev.js]]

// The user script that runs on the client side - this was thrown together in a couple hours
// Apache 2.0/CC-BY-SA
// (c) 2021 User:Ed6767 - full attributions https://github.com/wikimedia-gadgets/EasyWikiDev/commits/main/.wikidev/wikiscript.js

let hasRecievedFirstMessage = false;

const currentSkin = mw.config.get("skin");

const portletLinkSelector = {
	'vector': 'a > span',
	'vector-2022': 'a > span:nth-child(2)',
	'monobook': 'a',
	'timeless': 'a'
};

const currentPortletLinkSelector = portletLinkSelector[currentSkin];

const currentPageLink = mw.util.addPortletLink(
    'p-personal',
    '#',
    "Develop",
    "developLink",
    null, null, // ones we don't need
    '#pt-preferences' // put before preferences
);

currentPageLink.onclick = () => {
    if (hasRecievedFirstMessage) {
        stopDeveloperMode();
        window.location.reload(); // Clear the indev script
    } else {
        runDeveloperMode();
    }
};

function stopDeveloperMode() {
    localStorage.setItem('EasyWikiDev', 'disabled');
    currentPageLink.querySelector(currentPortletLinkSelector).innerText = "Develop";
}

function addCode(js){
  eval("window.debuggingMode=true;\n"+js+"\n//# sourceURL=EasyWikiDevScript.js;");
}

function runDeveloperMode() {
    currentPageLink.querySelector(currentPortletLinkSelector).innerText = "Stop Developing";

    // Create the localstorage note
    localStorage.setItem('EasyWikiDev', 'enabled');

    try {
        // Connect to the websocket at ws://localhost:6768
        const ws = new WebSocket("ws://localhost:6768");

        ws.onerror = e => { throw e; };

        ws.onclose = () => {
            mw.notify("EasyWikiDev: Websocket closed. The server may not be running, else try enabling developer mode again.", {
                title: "Socket closed",
                type: "error",
                autoHide: false,
            });
            stopDeveloperMode();
        };

        // When the websocket receives a message, it will run this function
        ws.onmessage = e => {
            if (!hasRecievedFirstMessage) {
                hasRecievedFirstMessage = true;

                // The message is the script.js file
                // It's just a string, so we can just eval it
                addCode(e.data);
            } else {
                window.location.reload();
            }
            
        }

    } catch (error) {
        console.error(error);
        mw.notify("EasyWikiDev: An error occured. Make sure the server is running, then check the Javascript console for more info.", {
            title: "Error",
            type: "error",
            autoHide: false,
        });
        stopDeveloperMode();
    }
}

// If present, run the developer mode
if (localStorage.getItem('EasyWikiDev') === 'enabled') {
    runDeveloperMode();
}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.