Greasy Fork

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-21 提交的版本,查看 最新版本

    // ==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.2.2
    // @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>
        ` );

        // Get div element with class "releases"
        var div = document.getElementsByClassName("releases")[0];

        // Create a new div element
        var newDiv = document.createElement("div");
        newDiv.className = "button-container";

        // Append newDiv to div
        div.appendChild(newDiv);

        // Move delete button with id "hapus" into newDiv
        var deleteButton = document.getElementById("hapus");
        newDiv.appendChild(deleteButton);

        // Delete button

        addButton('Save Bookmarks', saveBookmarks)
        addButton('Import Bookmarks', importBookmarks)
        })

        // Add a button next to an existing button with id "hapus"
        function addButton(text, onclick) {
            var button = document.createElement('button');
            button.innerHTML = text;
            button.onclick = onclick;
            // Make the button float right
            button.style.cssFloat = 'right';

            // Find the button with id "hapus"
            var hapus = document.getElementById('hapus');

            // Use the style of the "hapus" button, but change the colour to #913fe2
            button.setAttribute('style', button.style.cssText + '; color: #ffffff; background-color: #913fe2;');

            // Remove the button border
            button.style.border = 'none';
            button.style.borderRadius = '0px';

            // Give the button rounded borders
            button.style.borderTopLeftRadius = '3px';
            button.style.borderTopRightRadius = '3px';
            button.style.borderBottomLeftRadius = '3px';
            button.style.borderBottomRightRadius = '3px';

            // Make the button taller
            button.style.height = '24px';

            // Add a margin beside the button
            button.style.marginLeft = '10px';

            // Pad the space beside the button text
            button.style.paddingLeft = '20px';
            button.style.paddingRight = '20px';

            document.getElementById('hapus').parentNode.insertBefore(button, document.getElementById('hapus'));
        }

        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 = document.getElementById('filePick');
            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
            }
        }
    }());