Greasy Fork Bookmark

Bookmark the script easily

// ==UserScript==
// @name         Greasy Fork Bookmark
// @name:en      Greasy Fork Bookmark
// @name:ja      Greasy Fork ブックマーク
// @namespace    http://tampermonkey.net/
// @version      2024-04-24
// @description       Bookmark the script easily
// @description:en    Bookmark the script easily
// @description:ja    Greasy Forkのブックマーク機能
// @author       ぐらんぴ
// @match        https://greasyfork.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=greasyfork.org
// @grant        none
// @license      MIT
// ==/UserScript==

(()=>{
    var favs = JSON.parse(localStorage.getItem('favs')) || []
    function favLink(){
        let nav = document.querySelector("#site-nav > nav"),
            li = document.createElement('li')
        li.innerHTML = `<a href="https://greasyfork.org/Bookmarks">Favs</a>`
            nav.appendChild(li)
    }favLink()
    function favPage(){
        if(window.location.href == "https://greasyfork.org/Bookmarks"){
            document.title = 'bookmarks'
            document.querySelector("body > div > section").remove()

            let scripts = JSON.parse(localStorage.getItem("favs"))
            document.querySelector("body > div").innerHTML = `<ol id="browse-script-list" class="script-list ">`
            for(let script of scripts){
                let elm = document.querySelector("#browse-script-list"),
                    li = document.createElement("li")
                li.innerHTML = `
                <a class="script-link" href="${script.href}">${script.title}</a>
                <span class="script-description description">${script.description}`
                elm.appendChild(li);
            }
        }
    }favPage()
    function scriptPage(){
        if(location.href.match('/scripts')){
            let tabs = document.querySelector("#script-links"),
                li = document.createElement('li')
            li.innerHTML = `<a>☆</a>`
            tabs.appendChild(li)
            //
            let href = window.location.href,
                title = document.querySelector("#script-info > header > h2").innerText,
                index = -1,
                val = title,
                filteredObj = favs.find((item, i)=>{
                    if(item.title === val){
                        index = i;
                        return i;
                    }
                });
            if(index !== -1){
                li.innerHTML = '<a>★</a>'
            }
            li.addEventListener('click', ()=>{
                var value = {
                    title: title,
                    description: document.querySelector(".script-description").innerText,
                    href: href,
                }
                var index = -1,
                    val = title,
                    filteredObj = favs.find(function(item, i){
                        if(item.title === val){
                            index = i;
                            return i;
                        }
                    });
                if(index == -1){
                    favs.push(value)
                    li.innerHTML = '<a>★</a>'
                }else{
                    favs.splice(index, 1)
                    li.innerHTML = '<a>☆</a>'
                }
                localStorage.setItem('favs', JSON.stringify(favs))
            })
        }
    }scriptPage()
    function addStyle(){
        var css = `
        #script-links > li:nth-child(6) {
        text-decoration: none;
        cursor: pointer;
        color: #a42121;
        }`,
            style = document.createElement('style')
        style.innerHTML = css;
        document.head.append(style)
    }addStyle()
})();