Mycorrhiza Create Page Button

Adds a button create a page to your mycorrhiza header links

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Mycorrhiza Create Page Button
// @namespace    https://mycorrhiza.wiki
// @version      1.0
// @description  Adds a button create a page to your mycorrhiza header links
// @author       You
// @icon         https://em-content.zobj.net/source/microsoft/407/mushroom_1f344.png
//               NOTE: change this to your mycorrhiza instance.
// @match        https://mycorrhiza.wiki/*
// @grant        none
// @license       AGPL-3.0-or-later
// ==/UserScript==

(function() {
    'use strict';

    function addToHeader() {
        const ul = document.querySelector('.top-bar__highlights');
        if (!ul) return;

        const li = document.createElement('li');
        li.className = 'top-bar__highlight';

        const a = document.createElement('a');
        a.href = '#';
        a.className = 'top-bar__highlight-link';
        a.textContent = 'New hypha';
        a.addEventListener('click', function(event) {
            event.preventDefault();
            // TODO: I want to make this part of the UI instead of a prompt
            const hyphaName = prompt("New hypha name:");
            if (hyphaName) {
                window.location.href = `/edit/${encodeHyphaName(hyphaName)}`;
            }
        });

        li.appendChild(a);
        // INFO: replace prepend with appendChild if you want the button at the end of the list
        ul.prepend(li);
    }

    function encodeHyphaName(hyphaName) {
      let encodedHyphaName = hyphaName.replace(' ', '_')
      return encodedHyphaName
    }

    addToHeader();
})();