Persistent Invidious Settings

Makes Invidious settings persist across instances and in private browsing.

当前为 2023-04-23 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Persistent Invidious Settings
// @namespace    http://tampermonkey.net/
// @version      1.10
// @description  Makes Invidious settings persist across instances and in private browsing.
// @author       Veeno
// @license      GPLv3
// @resource     instances https://api.invidious.io/instances.json
// @match        https://*/*
// @icon         https://invidious.io/invidious-colored-vector.svg
// @run-at       document-start
// @grant        GM_getResourceText
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

/* jshint esversion: 11 */

(() => {
    'use strict';

    if(window.self !== window.top) return;

    const domain = location.hostname;

    const domainUpToDate = (() => {
        const storedDomainUpToDate = GM_getValue("Invidious_DomainUpToDate", {});
        try{
            return Object.fromEntries(
                JSON.parse(GM_getResourceText("instances"))
                    .filter(instance => instance[1].type === "https")
                    .map(instance => [instance[0], storedDomainUpToDate[instance[0]] || false])
            );
        }
        catch(e){
            return {};
        }
    })();

    if(!Object.hasOwn(domainUpToDate, domain)) return;

    function validateCurrentDomain(){
        domainUpToDate[domain] = true;
        GM_setValue("Invidious_DomainUpToDate", domainUpToDate);
    }

    function invalidateOtherDomains(){
        Object.keys(domainUpToDate).forEach(key => { domainUpToDate[key] = false; });
        validateCurrentDomain();
    }

    const storedSettings = GM_getValue(
       "Invidious_Settings",
        encodeURIComponent(JSON.stringify({
            annotations:                 false,
            annotations_subscribed:      false,
            autoplay:                    true,
            automatic_instance_redirect: false,
            captions:                    ["", "", ""],
            comments:                    ["youtube", ""],
            continue:                    false,
            continue_autoplay:           true,
            dark_mode:                   "",
            latest_only:                 false,
            listen:                      false,
            local:                       false,
            watch_history:               false,
            vr_mode:                     true,
            show_nick:                   false,
            locale:                      "en-US",
            region:                      "US",
            max_results:                 40,
            notifications_only:          false,
            player_style:                "invidious",
            quality:                     "hd720",
            quality_dash:                "auto",
            default_home:                "Popular",
            feed_menu:                   ["Popular", "Trending"],
            related_videos:              true,
            sort:                        "published",
            speed:                       1,
            thin_mode:                   false,
            unseen_only:                 false,
            video_loop:                  false,
            extend_desc:                 false,
            volume:                      100,
            save_player_pos:             false
        }))
    );

    const cookieSettings = document.cookie
                                   .split("; ")
                                   .find(entry => entry.startsWith("PREFS="))
                                   ?.slice(6);

    if(cookieSettings && domainUpToDate[domain]){
        if(cookieSettings !== storedSettings){
            GM_setValue("Invidious_Settings", cookieSettings);
            invalidateOtherDomains();
        }
    } else{
        const date = new Date();
        date.setFullYear(date.getFullYear() + 2);
        document.cookie = "PREFS=" + storedSettings + "; domain=" + domain + "; path=/; expires=" + date.toGMTString() + "; Secure; SameSite=Lax";
        validateCurrentDomain();
        location.reload();
    }
})();