New script kylebing.cn

1/8/2024, 12:18:59 PM

目前為 2024-01-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        New script kylebing.cn
// @namespace   Violentmonkey Scripts
// @match       https://kylebing.cn/tools/typepad/*
// @grant       none
// @version     1.0
// @author      -
// @description 1/8/2024, 12:18:59 PM
// @license MIT
// ==/UserScript==


(function() {
    'use strict';

    // 定义加载文件的函数
    function loadTextFile() {
        var fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.accept = 'text/plain';
        fileInput.style.display = 'none';

        fileInput.onchange = function(e) {
            var file = e.target.files[0];
            if (file) {
                var reader = new FileReader();
                var filename = e.target.files[0].name;
                console.log(filename);
                reader.onload = function(e) {
                    var editor = document.getElementById('editor');
                    if (editor) {
                        editor.value = e.target.result;
                    }
                    var titleedit = document.getElementsByClassName("editor-title")[0];
                    if (titleedit) {
                        titleedit.value = filename.split(".")[0];
                    }
                };
                reader.readAsText(file, 'UTF-8');
            }
            fileInput.remove();
        };

        document.body.appendChild(fileInput);
        fileInput.click();
    }

    // 创建按钮
    var btn = document.createElement('div');
    btn.className = 'btn';
    btn.id = "selectfile";
    btn.textContent = '加载文本文件';
    btn.addEventListener("click", function(){
      loadTextFile();
    });

    var observer = new MutationObserver(function(mutations, me) {
    var editor = document.getElementById('editor');
    if (editor) {
        var parentCont = document.getElementsByClassName("editor-toolbar")[0];
        var buttonGroup = document.createElement("div");
        buttonGroup.className = "btn-group";
        buttonGroup.appendChild(btn);
        parentCont.appendChild(buttonGroup);
        console.log("injected!!");
        me.disconnect(); // 停止观察
        return;
    }
});
    var config = { childList: true, subtree: true };
    var target = document.body;
    observer.observe(target, config);


    // TODO: 将按钮添加到页面的合适位置
    // 例如:
    // var container = document.querySelector('.your-container-selector');
    // container.appendChild(btn);
})();