Greasy Fork

Greasy Fork is available in English.

Asurascans bookmark saver

This script adds two buttons to the bookmarks page. One button will export the list of titles in your bookmarks, the other will import a list of titles and overwrite your bookmarks.

当前为 2022-07-20 提交的版本,查看 最新版本

// ==UserScript==
// @name        Asurascans bookmark saver
// @namespace   TestScript
// @author      Sieyk
// @grant       none
// @license     MIT
// @match       https://www.asurascans.com/bookmarks/
// @noframes
// @require     https://openuserjs.org/src/libs/ls18502857770.gmail.com/FileSaver.js
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @run-at      document-end
// @version     1.0
// @description  This script adds two buttons to the bookmarks page. One button will export the list of titles in your bookmarks, the other will import a list of titles and overwrite your bookmarks.
// ==/UserScript==
(function () {
    "use strict";

    window.addEventListener('load', () => {
        $("body").append ( `
    <input id="filePick" type="file" hidden=true>
    ` );
    addButton('Save Bookmarks', saveBookmarks, '7%')
    addButton('Import Bookmarks', importBookmarks, '4%')
    })

    function addButton(text, onclick, posY, cssObj) {
        cssObj = cssObj || {position: 'absolute', bottom: posY, left:'4%', 'z-index': 3}
        let button = document.createElement('button'), btnStyle = button.style
        document.body.appendChild(button)
        button.innerHTML = text
        button.onclick = onclick
        Object.keys(cssObj).forEach(key => (btnStyle[key] = cssObj[key]))
        return button
    }

    function saveBookmarks() {
        var blob = new Blob([localStorage.getItem("bookmark")],
                { type: "text/plain;charset=utf-8" });
        saveAs(blob, "bookmarks.txt")
    }

    async function importBookmarks() {
        // open file picker
        const input = event.srcElement.previousElementSibling.previousElementSibling;
        try {
            input.value = "";
            input.showPicker();
            console.debug(input.value);
            while (input.value == "") {
                await new Promise(r => setTimeout(r, 100));
            };
        } catch(error) {
            window.alert(input);
            return
        }
        console.debug(input.files);
        var reader = new FileReader();
        reader.readAsText(input.files[0]);
        while (reader.readyState != 2) {
            await new Promise(r => setTimeout(r, 100));
        }
        try {
        var arr = JSON.parse(reader.result);
        } catch(error) {
            window.alert("Invalid data type for bookmarks! Must be an Array.");
            return
        }
        if (Array.isArray(arr)) {
            localStorage.setItem("bookmark", reader.result);
            window.location.reload();
        }
        else {
            window.alert("Invalid data type for bookmarks! Must be an Array.");
            return
        }
    }
}());