Background Color Picker

Adds a color picker to the page and saves the selection in cookies

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Background Color Picker
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Adds a color picker to the page and saves the selection in cookies
// @author       George Kattenbelt
// @match        http://www.eveandersson.com/pi/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Helper functions for cookies
    function setCookie(name, value, days) {
        const d = new Date();
        d.setTime(d.getTime() + (days*24*60*60*1000));
        const expires = "expires="+ d.toUTCString();
        document.cookie = name + "=" + value + ";" + expires + ";path=/";
    }

    function getCookie(name) {
        const cname = name + "=";
        const decodedCookie = decodeURIComponent(document.cookie);
        const ca = decodedCookie.split(';');
        for(let i = 0; i < ca.length; i++) {
            let c = ca[i].trim();
            if (c.indexOf(cname) === 0) {
                return c.substring(cname.length, c.length);
            }
        }
        return "";
    }

    // Find the form
    const form = document.querySelector('body');
    if (!form) return; // Exit if form not found

    // Create label
    const label = document.createElement('label');
    label.textContent = "Background Color: ";
    label.style.marginLeft = "10px";

    // Create color input
    const colorInput = document.createElement('input');
    colorInput.type = 'color';
    colorInput.name = 'bgColor';

    // Load color from cookie if exists
    const savedColor = getCookie('bgColor');
    if (savedColor) {
        document.body.style.backgroundColor = savedColor;
        colorInput.value = savedColor;
    }

    // Update background and cookie when color changes
    colorInput.addEventListener('input', (e) => {
        const color = e.target.value;
        document.body.style.backgroundColor = color;
        setCookie('bgColor', color, 365);
    });

    // Append label and input at the end of the form
    const wrapper = document.createElement('div');
    wrapper.style.marginTop = "10px";
    wrapper.appendChild(label);
    wrapper.appendChild(colorInput);
    form.appendChild(wrapper);

})();