Mycorrhiza Create Page Button

Adds a button create a page to your mycorrhiza header links

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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();
})();