油猴/暴力猴工具換cook指令碼
// ==UserScript==
// @name 多賬號登入切換
// @namespace [email protected]
// @include http://2dgal.com/*
// @include http://bbs.2dgal.com/*
// @include *
// @version 1.0.1
// @grant none
// @description 可以快速在不同馬甲間切換
// ==/UserScript==
//介面
addStyle = function() {
var style = document.createElement('style');
style.type = 'text/css';
style.textContent = '\
.multiaccContainer{\
font-size:12px;\
position:fixed;\
z-index:1000001;\
width:10px;\
height:10px;\
background-color:green;\
border:black;\
top:30px;\
right:30px;\
inset 0 5px 0 rgba(255,255,255,0.3), 0 0 3px rgba(0,0,0,0.8);\
}\
.multiaccPanel{\
z-index:1000000;\
height:400px;\
width:200px;\
opacity:0.95;\
position:relative;\
right:190px;\
background:burlywood;\
}\
#multiaccSelect{\
width:120px;\
}\
.multiaccIntro{\
padding:5px;\
}\
.multiaccIntro em{\
color:#0099CC\
}'
var head = document.head;
head.appendChild(style);
}
insertDiv = function() {
var div = document.createElement('div');
div.className = 'multiaccContainer';
var panelDiv = document.createElement('div');
panelDiv.className = 'multiaccPanel';
panelDiv.innerHTML = '<button type=button id=multiaccSaveBtn class="multiaccBtn" >新建儲存</button>\
<button type=button id=multiaccUserDefine class="multiaccBtn" >自定義cookie</button>\
<a id=multiaccClosePanel href=javascript: >×</a>\
<br><div><button type=button id=multiaccSwitch class="multiaccBtn" >切換至</button>\
<select id=multiaccSelect ></select></div>\
<button type=button id=multiaccRename class="multiaccBtn" >重新命名</button><br><hr>\
<button type=button id=multiaccImport class="multiaccBtn" >匯入</button>\
<button type=button id=multiaccExport class="multiaccBtn" >匯出</button>\
<button type=button id=multiaccDelete class="multiaccBtn" >刪除</button>\
<div class="multiaccIntro" >使用說明:<br>\
<p>1. 多數論壇只需每個ID新建儲存一次,就可以切換</p>\
<p>2. 部分論壇cookie是繫結UA的,UA改了cookie失效,匯入匯出也沒用了</p>\
一些網站、論壇 cookie使用了HTTP only 引數,\
指令碼是不能直接讀取、修改那些cookie的,如<em>百度貼吧,緋月</em>,\
這時需要一些小技巧:<a href="https://greasyfork.org/zh-CN/scripts/17322-%E5%A4%9A%E8%B4%A6%E5%8F%B7%E7%99%BB%E5%BD%95%E5%88%87%E6%8D%A2">點選開啟</a>\
</div>'
panelDiv.style.display = 'none';
div.addEventListener('click', function(e) {
panelDiv.style.display = 'block';
}, false);
div.appendChild(panelDiv);
document.body.appendChild(div);
return div;
}
updateOption = function() {
var t='';
for (var p in cookieData){
t+='<option value="' + p + '" >' + p + '</option>';
}
document.getElementById('multiaccSelect').innerHTML=t;
}
// 儲存 讀取 匯入 切換 刪除cookie
// cookieData={ name:cookie,...}
loadData = function() {
try {
return JSON.parse(localStorage["multiacc"]);
} catch (e) {
localStorage["multiacc"] = "";
return {};
}
}
saveData = function() {
localStorage["multiacc"] = JSON.stringify(cookieData);
}
importCookie = function(cookie) {
if (!cookie) cookie = document.cookie; //普通的儲存
var selected = document.getElementById('multiaccSelect');
var defaultText = selected.value ? selected.value : document.title
var name = prompt('記錄名, 相同則覆蓋', defaultText);
if (!name) return; //空名字不儲存
cookieData[name] = cookie;
saveData();
updateOption();
}
deleteCookie = function(name){
if(name){
delete cookieData[name];
saveData();
updateOption();
}
}
changeActiveCookie = function(cookie) {
var cookieArray = cookie.split(';');
var d = new Date();
d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000)); //一個月
var expires = "expires=" + d.toUTCString();
for (var i = 0; i < cookieArray.length; i++) {
document.cookie = cookieArray[i] + '; path=/; ' + d;
}
}
//繫結介面事件
multiaccEvent = function() {
document.getElementById("multiaccSaveBtn").addEventListener('click', function(e) {
importCookie();
}, false);
document.getElementById("multiaccSwitch").addEventListener('click', function(e) {
var name=document.getElementById('multiaccSelect').value;
changeActiveCookie(cookieData[name]);
}, false);
document.getElementById("multiaccRename").addEventListener('click', function(e) {
var name=document.getElementById('multiaccSelect').value;
var newName = prompt('重新命名為',name);
if(newName){
cookieData[newName]=cookieData[name];
delete cookieData[name];
saveData();
updateOption();
}
}, false);
document.getElementById("multiaccUserDefine").addEventListener('click', function(e) {
var cookie = prompt('輸入cookie');
if (cookie) {
importCookie(cookie);
}
}, false);
document.getElementById("multiaccImport").addEventListener('click', function(e) {
var cookie = prompt('輸入cookie');
if (cookie) {
try{
importCookieData=JSON.parse(cookie);
for(var name in importCookieData){
cookieData[name]=importCookieData[name];
}
saveData();
updateOption();
}
catch(e){
alert("匯入資料錯誤");
}
}
}, false);
document.getElementById("multiaccExport").addEventListener('click', function(e) {
prompt('複製資料儲存', JSON.stringify(cookieData));
}, false);
document.getElementById("multiaccDelete").addEventListener('click', function(e) {
var name=document.getElementById('multiaccSelect').value;
if(confirm("確定刪除"+name+"?")){
deleteCookie(name);
}
}, false);
document.getElementById('multiaccClosePanel').addEventListener('click', function (e) {
e.stopPropagation();
document.getElementsByClassName('multiaccPanel')[0].style.display = 'none';
}, false);
}
// init
cookieData = loadData();
addStyle();
insertDiv();
updateOption();
multiaccEvent();