CookieHelper

GM_cookie API Test

目前為 2023-11-13 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         CookieHelper
// @namespace    https://github.com/cloudswave
// @version      0.1.7
// @description  GM_cookie API Test
// @author       ethan
// @match        *://*/*
// @grant        GM_cookie
// @license      MIT
// ==/UserScript==

var panel = document.createElement('div');
var getListBtn = document.createElement('div');
var setBtn = document.createElement('div');
var deleteBtn = document.createElement('div');
var ui = [panel,getListBtn,setBtn,deleteBtn];
function showPanel(show){
    if(show) panel.style.display = 'block';
    else panel.style.display = 'none';
}
function showLog(str){
    showPanel(true);
    panel.innerHTML += str + "<br>";
    console.log(str);
}

function getCookieList(){
    try{
        showLog(`start GM_cookie.list`);
        GM_cookie.list({}, (cookies, error) => {
            if(error) {
                showLog(`error:${error}`);
                return;
            }
            showLog(`cookies:${cookies}`);
            if(!cookies) {
                return;
            }
            let cookieStr = "";
            for (let i = 0; i < cookies.length; i++) {
                cookieStr += `${cookies[i].name}=${cookies[i].value};`;
            }
            showLog(cookieStr == "" ? "no cookie" : cookieStr);
        });
    } catch(e){
        showLog(`error:${e}`);
    }
}

function setCookieTest(){
    let cookie = {
          name: 'test',
          value: '123',
          path: '/',
          expirationDate: Math.floor(Date.now() / 1000) + (60 * 60 * 24 * 30) // Expires in 30 days
    };
    showLog(`start GM_cookie.set cookie ${JSON.stringify(cookie)}`);
    GM_cookie.set(cookie, function(error) {
          if (error) {
            showLog(`error:${error}`);
          } else {
            showLog('Cookie set successfully.');
          }
    });
}

function delCookieTest(){
    showLog(`start GM_cookie.delete cookie test`);
    GM_cookie.delete({ name: 'test' }, function(error) {
        if (error) {
            showLog(`error:${error}`);
        } else {
            showLog('Cookie deleted successfully');
        }
    });
}


function createUI() {
	// 创建悬浮按钮
    let btnStyle = "font-size:small;background: red;position: fixed;bottom: 60px;right: 20px;color: white;z-index: 9999;padding: 5px;";
	getListBtn.innerHTML = 'GM_cookie.list';
    getListBtn.style = btnStyle;
    getListBtn.style.bottom = "60px";
	document.body.appendChild(getListBtn);

    setBtn.innerHTML = 'GM_cookie.set';
    setBtn.style = btnStyle;
    setBtn.style.bottom = "90px";
	document.body.appendChild(setBtn);

    deleteBtn.innerHTML = 'GM_cookie.delete';
    deleteBtn.style = btnStyle;
    deleteBtn.style.bottom = "120px";
	document.body.appendChild(deleteBtn);

	// 创建浮层
	panel.innerHTML = '';
	panel.style = "font-size:small;position: fixed; bottom: 0px; width: 60%; max-height: 90%; background-color: rgba(0, 0, 0, 0.8); color: white; padding: 20px; z-index: 10000; display: none; word-wrap: break-word; overflow-y: auto;"
	document.body.appendChild(panel);

	// 按钮点击事件监听
	getListBtn.addEventListener('click', getCookieList);
	setBtn.addEventListener('click', setCookieTest);
	deleteBtn.addEventListener('click', delCookieTest);
	// 点击浮层外部隐藏浮层
	document.addEventListener('click', function(e) {
        for (let i = 0; i < ui.length; i++) {
            if(e.target === ui[i]) return;
        }
        showPanel(false);
	});
}
(function() {
	'use strict';
	createUI();
})();