网购查券小帮手,不花冤枉钱,可自动查询淘宝、天猫、京东、唯品会、天猫超市等隐藏的大额优惠券和优惠活动。脚本持续维护更新~

网购查券小帮手,不花冤枉钱!!!支持平台:京东、淘宝、天猫、天猫超市、天猫国际、京东国际、京东图书、京东大药房、阿里大药房、唯品会等;功能:1、搜索商品时会自动查询标注有优惠券和活动的商品,无需进入详情页,方便快捷;2、浏览商品详情页时脚本会自动查询商品是否有隐藏的优惠券;3、浏览记录标注(本地存储、可手动清空);4、网页显示优化;脚本长期维护更新,请放心使用~

// ==UserScript==
// @name              网购查券小帮手,不花冤枉钱,可自动查询淘宝、天猫、京东、唯品会、天猫超市等隐藏的大额优惠券和优惠活动。脚本持续维护更新~
// @name:zh           网购查券小帮手,不花冤枉钱,可自动查询淘宝、天猫、京东、唯品会、天猫超市等隐藏的大额优惠券和优惠活动。脚本持续维护更新~
// @name:zh-TW        網購查券小幫手,不花冤枉錢,可自動查詢淘寶、天貓、京東、唯品會、天貓超市等隱藏的大額優惠券和優惠活動。指令碼或直譯式程式持續維護更新~
// @description       网购查券小帮手,不花冤枉钱!!!支持平台:京东、淘宝、天猫、天猫超市、天猫国际、京东国际、京东图书、京东大药房、阿里大药房、唯品会等;功能:1、搜索商品时会自动查询标注有优惠券和活动的商品,无需进入详情页,方便快捷;2、浏览商品详情页时脚本会自动查询商品是否有隐藏的优惠券;3、浏览记录标注(本地存储、可手动清空);4、网页显示优化;脚本长期维护更新,请放心使用~
// @description:zh    网购查券小帮手,不花冤枉钱!!!支持平台:京东、淘宝、天猫、天猫超市、天猫国际、京东国际、京东图书、京东大药房、阿里大药房、唯品会等;功能:1、搜索商品时会自动查询标注有优惠券和活动的商品,无需进入详情页,方便快捷;2、浏览商品详情页时脚本会自动查询商品是否有隐藏的优惠券;3、浏览记录标注(本地存储、可手动清空);4、网页显示优化;脚本长期维护更新,请放心使用~
// @description:zh-TW 網購查券小幫手,不花冤枉錢!!!支援平台:京東、淘寶、天貓、天貓超市、天貓國際、京東國際、京東圖書、京東大藥房、阿里大藥房、唯品會等;功能:1、搜索商品時會自動查詢標註有優惠券和活動的商品,無需進入詳情頁,方便快捷;2、瀏覽商品詳情頁時指令碼或直譯式程式會自動查詢商品是否有隱藏的優惠券;3、瀏覽記錄標註(本地存儲、可手動清空);4、網頁顯示優化;指令碼或直譯式程式長期維護更新,請放心使用~
// @icon		      data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAklJREFUWEfVl6tOA0EUhs+GOwkCX0hDQiEQHqBV9RTHxSCQaFBggAcAjQTJzdUQDEHQBDQkFEMC78CtZMnPcNqzw9xolzSM2U53uuc7//nP7DSK4zimNo7oXwHcPhDdPMSEKwZfJ7JqjutkNvq6ho4gBRDo6LwROOThs0WiuWLkXeoEaCawjBgCYQVA8K29dPy5sWQvixHgN8E7O1TOfT2N3PFd7UPN32tEL29ENggjwMKmP3MEQVAG8BX7+ZVobTGi8eHkyh8AMNvxuftxA/3hgeWTckNEK/NJYyYAXMHRWlMjEXV3+XJV98uXZhUBABAeCQCX9Aeb/paSaMvbZgBdhTqAz3gAqD4SlStuf5TyKkMbACClCnUAl/y93UT76xGVK3ZpOftSIaJS3g0wnSeaKShF6wDoed5apZQIDrfvrqYHIMvgBRgcUDgAwEAZXIMN5iqBEcBkQPQ4Wo4BELz6pOaQmefycy5DXg/IhOoKmABYfv6B9ACMtHOoDCk/h3jACGDygNxwUAJkjKCQEG5nANxjyUMAgj2gA7AHuM7sB9l2TQOY2pANmHYJjG1o2oj+CsC4ESFL3Qe2EmAtZJcl0MvjakNu6cRGhImuggRAbdFiIQOtansZSfl/AOgqNPvatUHqwY0AUgW5EYVk7lsjpee13iNZWiro5wAngPRDGirYghtLoMuI/eH0qvkjGJ8PbOUJ+mNychHT2bWvwo37vFXLo1dLAPxj9P3dY0z3329EfR8YzRCNDSXPfD7sIAV8D2nlftsBPgHDwZmwII4MigAAAABJRU5ErkJggg==
// @namespace         chaowantianxia_coupon_downloader
// @version           1.0.4
// @author            潮玩天下
// @match             *://*.taobao.com/*
// @match             *://*.tmall.com/*
// @match             *://*.tmall.hk/*
// @match             *://*.liangxinyao.com/*
// @match             *://chaoshi.detail.tmall.com/*
// @match             *://pages.tmall.com/wow/an/cs/search**
// @match             *://*.jd.com/*
// @match             *://*.jd.hk/*
// @match             *://item.jkcsjd.com/*
// @match             *://*.yiyaojd.com/*
// @match             *://www.vipglobal.hk
// @match             *://*.vip.com/*
// @match             *://detail.vip.com/detail-*
// @match             *://www.vipglobal.hk/detail-*
// @match             *://category.vip.com/suggest.php**
// @match             *://list.vip.com/*.html
// @exclude           *://map.taobao.com/*
// @exclude           *://creator.guanghe.taobao.com/*
// @exclude           *://myseller.taobao.com/*
// @exclude           *://qn.taobao.com/*
// @exclude           *://jianghu.taobao.com/*
// @exclude           *://uland.taobao.com/*
// @exclude           *://jingfen.jd.com/*
// @exclude           *://passport.jd.com/*
// @exclude           *://jmw.jd.com/*
// @exclude           *://login.taobao.com/*
// @exclude           *://passport.shop.jd.com/*
// @exclude           *://huodong.taobao.com/wow/z/guang/gg_publish/*
// @exclude           *://passport.vip.com/*
// @connect           t.jtm.pub
// @grant             GM_openInTab
// @grant             GM.openInTab
// @grant             GM_getValue
// @grant             GM.getValue
// @grant             GM_setValue
// @grant             GM.setValue
// @grant             GM_addStyle
// @grant             GM_xmlhttpRequest
// @grant             GM.xmlHttpRequest
// @grant             GM_registerMenuCommand
// @license           AGPL License
// @antifeature  	  referral-link 【此提示为GreasyFork代码规范要求含有查券功能的脚本必须添加,请知悉!】
// @charset		      UTF-8
// @run-at            document-idle
// ==/UserScript==

(function() {
	'use strict';
	
	/**
	 * AGPL 协议全称为 GNU Affero General Public License(GNU Affero 通用公共许可证),它是一种自由软件许可证。
	 * AGPL 协议的主要特点是在传统的开源软件许可证(如 GPL)的基础上,加强了对软件在网络环境中使用的限制。
	 * GPL 是 GNU General Public License(GNU 通用公共许可证)的缩写,本脚本遵循AGPL协议!满足其开源原则、Copyleft原则、附带源代码、允许收费等原则
	 * 
	 * 脚本中部分工具类搜索自互联网,并没有明确的出处。因此在此说明!如有侵权请留言告知!以上~
	 * 领券部分源码借鉴自:https://github.com/huahuacatTX/greasyfork/blob/main/pro_huahuacat_union.js
	 * 特此声明!
	 */
	
	const browsingLocalStorageKey = "browsing_history_local_storage_key";
	const details = ["detail.tmall.com", "item.taobao.com", "item.jd.com", "item.yiyaojd.com", "npcitem.jd.hk","detail.tmall.hk", "detail.vip.com", "item.jkcsjd.com"];
	const browsedHtml = `<div style="position:absolute;white-space: nowrap; top:7px;padding:2px 5px;font-size:11px;background-color:rgb(3,106,251);color:#FFF;z-index:9999999999;border-radius:20px;right:10px;"><b>已浏览</b></div>`;
	
	function quickSort(arr) {
		if (arr.length <= 1) {
			return arr;
		}
		const pivot = arr[arr.length - 1], left = [], right = [];
		for (let i = 0; i < arr.length - 1; i++) {
			if (arr[i] < pivot) {
				left.push(arr[i]);
			} else {
				right.push(arr[i]);
			}
		}
		return [...quickSort(left), pivot,...quickSort(right)];
	}
	
	function obtainParameterBySuffix(url=window.location.href, suffix = "html"){
		if(url.indexOf("?")!=-1) url = url.split("?")[0];
		if(url.indexOf("#")!=-1) url = url.split("#")[0];
		const urls = url.split("/");
		return urls[urls.length-1].replace(`.${suffix}`, "");
	}
	
	//时间格式化
	function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0');
		const day = String(date.getDate()).padStart(2, '0');
		const hours = String(date.getHours()).padStart(2, '0');
		const minutes = String(date.getMinutes()).padStart(2, '0');
		const seconds = String(date.getSeconds()).padStart(2, '0');
		return format
			.replace('YYYY', year)
			.replace('MM', month)
			.replace('DD', day)
			.replace('HH', hours)
			.replace('mm', minutes)
			.replace('ss', seconds);
	}
	
	function isArray(obj) {
		return Array.isArray(obj);
	}
	
	function isObject(obj) {
		return typeof obj === 'object' && obj!== null &&!Array.isArray(obj);
	}
	
	function getSearchParameter(paramsString=window.location.href, tag) {
		if (paramsString.indexOf("?")!== -1) {
			paramsString = paramsString.split('?')[1];
		}
		return (new URLSearchParams(paramsString)).get(tag);
	}
	
	function randomInt(min, max) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}
	
	//tampermonkey网络请求方法
	function makeRequest(method, url, param) {
		return new Promise((resolve, reject) => {
			GM_xmlhttpRequest({
				url: url,
				method: method,
				data: param,
				onload: function (response) {
					const status = response.status;
					if (status === 200 || status === "200") {
						const responseText = response.responseText;
						resolve({ "code": "ok", "result": responseText });
					} else {
						reject({ "code": "exception", "result": null });
					}
				}
			});
		});
	}
	
	//防抖函数
	function debounce(func, delay) {
		let timer;
		return function() {
			clearTimeout(timer);
			timer = setTimeout(() => {
				func.apply(this, arguments);
			}, delay);
		};
	}
		
	async function findElementAsync(selector, target = document.body, allowEmpty = true, delay = 10, maxDelay = 10 * 1000) {
		return new Promise((resolve, reject) => {
			if (selector.toUpperCase() === "BODY") {
				resolve(document.body);
				return;
			}
			if (selector.toUpperCase() === "HTML") {
				resolve(document.html);
				return;
			}
			let totalDelay = 0;
			let element = target.querySelector(selector);
			let result = allowEmpty?!!element : (!!element &&!!element.innerHTML);
			if (result) {
				resolve(element);
				return;
			}
			const interval = setInterval(() => {
				if (totalDelay >= maxDelay) {
					clearInterval(interval);
					resolve(null);
					return;
				}
				element = target.querySelector(selector);
				result = allowEmpty?!!element : (!!element &&!!element.innerHTML);
				if (result) {
					clearInterval(interval);
					resolve(element);
				} else {
					totalDelay += delay;
				}
			}, delay);
		});
	}
	
	function hashFunction(str) {
		let hash = 0;
		for (let i = 0; i < str.length; i++) {
			hash = ((hash << 5) - hash) + str.charCodeAt(i);
			hash &= hash; // Convert to 32bit integer
		}
		return hash;
	}
	
	function getLocalStorageValue(name, value=null) {
		let storageValue = value;
		if (typeof GM_getValue === "function") {
			storageValue = GM_getValue(name, value);
		} else if(typeof GM.setValue === "function"){
			storageValue = GM.getValue(name, value);
		}else{
			var arr = window.localStorage.getItem(name);
			if(arr != null){
				storageValue = arr
			}
		}
		return storageValue;
	}
	
	function setLocalStorageValue(name, value){
		if (typeof GM_setValue === "function") {
			GM_setValue(name, value);
		} else if(typeof GM.setValue === "function"){
			GM.setValue(name, value);
		}else{
			window.localStorage.setItem(name, value)
		}
	}
	
	function openInTab(url, options={"active":true, "insert":true, "setParent":true}){
		if (typeof GM_openInTab === "function") {
			GM_openInTab(url, options);
		} else {
			GM.openInTab(url, options);
		}
	}
	
	function getPlatform(url = window.location.host){
		let platform = "";
		if([/tmall.com/, /tmall\.hk/].map((reg)=> reg.test(url)).some((re)=>re)){
			platform = "tmall";
		}else if([/taobao\.com/, /liangxinyao\.com/].map((reg)=> reg.test(url)).some((re)=>re)){
			platform = "taobao";
		}else if([/jd\.com/, /jd\.hk/, /yiyaojd\.com/, /jkcsjd\.com/].map((reg)=> reg.test(url)).some((re)=>re)){
			platform = "jd";
		}else if([/vip\.com/, /vipglobal\.hk/].map((reg)=> reg.test(url)).some((re)=>re)){
			platform = "vpinhui";
		}
		return platform;
	}
	
	function encodeTitle(title){
		if(!title){
			return "";
		}
		return encodeURIComponent(title.replace(/\t|\r/g,""));
	}
	
	GM_registerMenuCommand("清除商品标记记录", ()=> {
		if(confirm('此弹窗来自脚本-网购助手\n是否要移除所有的浏览记录?移除后将不可恢复...')){
			setLocalStorageValue(browsingLocalStorageKey,[]); //已浏览标识
		}
	});
	
	const CouponDiscoverer = {
		generateResultFlag:true,
		isActive:function(){
			const currentHost = window.location.host;
			return details.map((host)=>currentHost.indexOf(host)!=-1).some((result)=>result);
		},
		getGoodsInfo:function(platform){	
			var goodsId = "";
			var goodsName = "";
			const href = window.location.href;
			if(platform=="taobao"){
				goodsId = getSearchParameter(window.location.search, "id");
				try{
					const titleObj = document.querySelector("[class^='ItemTitle--']");
					if(!!titleObj){
						goodsName = titleObj.textContent;
					}
				}catch(e){}
				
			}else if(platform=="tmall"){
				goodsId = getSearchParameter(window.location.search, "id");
				try{
					const titleObj = document.querySelector("[class^='ItemTitle--']");
					if(!!titleObj){
						goodsName = titleObj.textContent;
					}
				}catch(e){}
				
			}else if(platform=="jd"){
				goodsId = obtainParameterBySuffix(href);
				try{
					const titleObj = document.querySelector("[class='sku-name']");
					if(!!titleObj){
						goodsName = titleObj.textContent;
					}
				}catch(e){}
			}else if(platform=="vpinhui"){
				goodsId = obtainParameterBySuffix(href).replace("detail-","");
				const titleObj = document.querySelector("[class='pib-title-detail']");
				if(!!titleObj){
					goodsName = titleObj.textContent;
				}
			}
			return {"goodsId":goodsId, "goodsName":encodeTitle(goodsName)};
		},
		browsingHistory:function(platform, goodsId){
			let histories = getLocalStorageValue(browsingLocalStorageKey, []);
			let saveContent = platform+"_"+goodsId;
			if(!histories.includes(saveContent)){
				histories.unshift(saveContent);
				setLocalStorageValue(browsingLocalStorageKey, histories.slice(0,60));
			}
		},
		getHandlerElement:async function(handler){
			const getElement = async (handler)=>{
				const promiseArray = [];
				const handlers = handler.split("@");
				for(let i=0; i<handlers.length; i++){
					const eleName = handlers[i];
					if(!eleName){
						continue;
					}
					if(eleName=="body"){
						promiseArray.push(
							new Promise((resolve,reject) =>{ resolve(document.body) }) 
						);
					}else if(eleName=="html"){
						promiseArray.push(
							new Promise((resolve,reject) =>{ resolve(document.html) }) 
						);
					}else{
						promiseArray.push(findElementAsync(eleName, document.body, true, 10, 1500));
					}
				}
				const element = await Promise.race(promiseArray);
				return element ? element : null;
			}
		
			const element = await getElement(handler);
			return new Promise((resolve,reject) =>{
				resolve(element);
			});
		},
		generateHtml:async function(platform, goodsId, goodsName){
			if(!platform || !goodsId){
				return "kong";
			}
			let addition = "";
			if(platform=="vpinhui"){
				const vip = goodsId.split("-");
				addition = vip[0];
				goodsId = vip[1];
			}
			
			this.browsingHistory(platform, goodsId);
			const goodsCouponUrl = "https://t.jtm.pub/api/coupon/query?no=3&version=1.0.2&platform="+platform+"&id="+goodsId+"&q="+goodsName+"&addition="+addition;
			try{
				const data = await makeRequest("GET", goodsCouponUrl, null);
				if(data.code=="ok" && !!data.result){
					const json = JSON.parse(data.result);
					await this.generateCoupon(platform, json.data);
					await this.generateQrcode(platform, json.mscan);
					//开启插入检测
					let heartms = 0;
					const HEART_DELAY = 1500, MAX_MS = 1000*30;  
					const generateResultInterval = setInterval(async ()=>{
						if(this.generateResultFlag){
							if(document.querySelector("*[name='exist-llkbccxs-9246-hi']") || heartms>=MAX_MS){
								clearInterval(generateResultInterval);
							}else{
								await this.generateCoupon(platform, json.data);
							}
						}
						heartms += HEART_DELAY;
					}, HEART_DELAY);
				}
			}catch(e){console.log(e);}
		},
		generateCoupon:async function(platform, result){
			try{
				this.generateResultFlag = false;
				if(!result || result==="null" || !result.hasOwnProperty("css") || !result.hasOwnProperty("html") || !result.hasOwnProperty("handler")){
					return;
				}
				
				const {css, html, handler, templateId} = result;
				if(!css || !html || !handler){
					return;
				}
				GM_addStyle(css);
			
				// 添加HTML, 需要动态检测元素
				const handlerElement = await this.getHandlerElement(handler);
				if(!handlerElement){
					return;
				}
				if(platform=="taobao"){
					handlerElement.parentNode.insertAdjacentHTML('afterend', html);
				}else if(platform=="tmall"){
					handlerElement.parentNode.insertAdjacentHTML('afterend', html);
				}else if(platform=="jd"){
					handlerElement.insertAdjacentHTML('afterend', html);
				}else if(platform=="vpinhui"){
					handlerElement.insertAdjacentHTML('afterend', html);
				}
				
				const templateElement = document.querySelector("div[id='"+templateId+"']");
				if(!templateElement){
					return;
				}
				
				const couponId = templateElement.getAttribute("data-id");
				const goodsPrivateUrl = "https://t.jtm.pub/api/private/change/coupon?no=3&v=1.0.2&platform="+platform+"&id=";
				if(!/\d/.test(couponId)){
					return;
				}
				
				setInterval(()=>{
					templateElement.querySelectorAll("*").forEach((element)=>{
						element.removeAttribute("data-spm-anchor-id");
					});
				},400);
				
				const couponElementA = templateElement.querySelector("a[name='cpShUrl']"), clickedTag = "aclicked";
				if(couponElementA){
					couponElementA.addEventListener("click",()=>{
						event.stopPropagation();
						event.preventDefault();
						if(couponElementA.getAttribute(clickedTag)){
							return;
						}
						couponElementA.setAttribute(clickedTag, "true");
						const href = couponElementA.getAttribute("href");
						if(href && href.indexOf("https://")!=-1){
							openInTab(href);
							couponElementA.removeAttribute(clickedTag);
						}else{
							makeRequest("GET", goodsPrivateUrl+couponId, null).then((privateResultData)=>{
								if(privateResultData.code==="ok" && !!privateResultData.result){
									let url = JSON.parse(privateResultData.result).url;
									if(url){
										openInTab(url);
									}
								}
								couponElementA.removeAttribute(clickedTag);
							}).then(()=>{
								couponElementA.removeAttribute(clickedTag);
							});
						}
					});
				}
			
				const canvasElement = document.querySelector("#ca"+templateId);
				if(!canvasElement){
					return;
				}
				const qrcodeResultData = await makeRequest("GET", goodsPrivateUrl+couponId, null);
				if(!!qrcodeResultData && qrcodeResultData.code==="ok" && !!qrcodeResultData.result){
					let img = JSON.parse(qrcodeResultData.result).img;
					if(!!img){
						let cxt = canvasElement.getContext("2d");
						let imgData = new Image();
						imgData.src = img;
						imgData.onload=function(){
							cxt.drawImage(imgData, 0, 0, imgData.width, imgData.height);
						}
					}
				}
			}catch(e){
				console.log(e);
			}finally{
				this.generateResultFlag = true;
			}
		},
		generateQrcode:async function(platform, mscan){
			if(!mscan || mscan==="null" || !mscan.hasOwnProperty("mount")
				|| !mscan.hasOwnProperty("html")|| !mscan.hasOwnProperty("qrcode")){
				return;
			}
			const {mount, html, qrcode, iden} = mscan;
			if(!!mount && !!html && !!qrcode && !!iden){
				const mountElement = await findElementAsync(mount, document.body, true, 10, 1500);
				if(mountElement){
					mountElement.insertAdjacentHTML('afterend', html);
					let canvasElement = document.getElementById("mscan"+iden);
					let width = canvasElement.getAttribute("width");
					let height = canvasElement.getAttribute("height");
					let cxt = canvasElement.getContext("2d");
					let imgData = new Image();
					imgData.src = qrcode;
					imgData.onload=function(){
						cxt.drawImage(imgData, 0, 0, width, height);
					}
				}
			}
		},
		manageSkuConstraints:function(platform){ //如果sku太多就折叠
			if(platform=="tmall" || platform=="taobao"){
				findElementAsync("[class='skuItemWrapper']", document.body, false, 10, 1500).then((skuItemWrapper)=>{
					if(skuItemWrapper != null){
						const { style } = skuItemWrapper;
						style.maxHeight = "400px";
						style.overflow = "auto";
					}
				}).catch(()=>{console.log(e);});
			}else if(platform=="jd"){
				const skuItemWrapper = document.querySelector("#choose-attrs");
				if(skuItemWrapper){
					const { style } = skuItemWrapper;
					style.maxHeight = "400px";
					style.overflow = "auto";
				}
			}
		},
		start:function(){
			if(!this.isActive()){
				return;
			}
			const platform = getPlatform();
			if(!platform){
				return;
			}
			this.manageSkuConstraints(platform);
			const goodsData = this.getGoodsInfo(platform);
			this.generateHtml(platform, goodsData.goodsId, goodsData.goodsName);
		}
	};
	
	/**
	 * 模拟map数据结构
	 */
	class MyMap {
		constructor() {
			this.map = {};
		}
		set(key, value) {
			this.map[key] = value;
		}
		get(key) {
			return this.map[key];
		}
		has(key) {
			return key in this.map;
		}
		delete(key) {
			if (this.has(key)) {
				delete this.map[key];
				return true;
			}
			return false;
		}
		keys() {
			return Object.keys(this.map);
		}
		values() {
			return Object.values(this.map);
		}
		entries() {
			return Object.entries(this.map);
		}
	}

	const CouponSearcher = {
		intervalCompleted:true,
		getHistories:function(){
			return getLocalStorageValue(browsingLocalStorageKey, []);
		},
		pickupsoso:function(){ //收集列表的元素
			const selectorElementList = new Array();
			const url = window.location.href;
			if(url.indexOf("://s.taobao.com/search")!=-1 || url.indexOf("://s.taobao.com/list")!=-1) { //淘宝搜索页面
				selectorElementList.push({"element":".items >.item", "page":"taobao"});
		
		        selectorElementList.push({"element":".Content--content--sgSCZ12 .Card--doubleCardWrapper--L2XFE73", "page":"taobao_new"});
				selectorElementList.push({"element":".Content--content--sgSCZ12 .Card--listCardWrapper--ax09OfT", "page":"taobao_new"});
				selectorElementList.push({"element":".Content--content--sgSCZ12 .Card--doubleCardWrapperMall--uPmo5Bz", "page":"taobao_new"});
				selectorElementList.push({"element":".Content--content--sgSCZ12 .Card--listCardWrapperMall--E3jI8L4", "page":"taobao_new"});
		
			}else if(url.indexOf("taobao.com/category")!=-1){ //淘宝店铺分类
				selectorElementList.push({"element":"dl.item", "page":"taobao_category"});
			}else if(url.indexOf("taobao.com/search.htm")!=-1){ //淘宝店铺搜索
				selectorElementList.push({"element":"dl.item", "page":"taobao_shop_search"});
			}
			else if (url.indexOf("://list.tmall.com/search_product.htm")!=-1 || url.indexOf("://list.tmall.com//search_product.htm")!=-1
				|| url.indexOf("://list.tmall.com/coudan/search_product.htm")!=-1
				|| url.indexOf("://pages.tmall.com/wow/an/cs/search")!=-1) { //天猫超市
				selectorElementList.push({"element":".product", "page":"tmall_chaoshi_search"});
				selectorElementList.push({"element":".chaoshi-recommend-list .chaoshi-recommend-item", "page":"tmall_chaoshi_search_new"}); //新天猫超市
				selectorElementList.push({"element":".feeds-list .feeds-item", "page":"tmall_chaoshi_search_20240115"}); //天猫超市新页面https://pages.tmall.com/wow/an/cs/search%2F18a4077f847?spm=a3204.7084713.1996500281.13.z3hXwd&s=0&user_id=725677994&area_code=330100&cat=50514029&active=1&style=g&acm=lb-zebra-27092-331852.1003.4.457104&search_condition=1&sort=s&scm=1003.4.lb-zebra-27092-331852.OTHER_14434954372593_457104&n=40#/?q=%E7%89%99%E8%86%8F&sort=s&page_no=1&spos=&brand=&prop=&cat=50514008
		
			}else if (url.indexOf("//list.tmall.hk/search_product")!=-1) { //天猫国际
				selectorElementList.push({"element":"#J_ItemList .product", "page":"tmall_hk"});
			}else if(url.indexOf("tmall.com/category-")!=-1){ //天猫分类
				selectorElementList.push({"element":"#J_ShopSearchResult dl.item", "page":"tmall_category"});
			}else if(url.indexOf("tmall.com/category-")!=-1){ //天猫旗舰店分类
				selectorElementList.push({"element":"#J_ShopSearchResult dl.item", "page":"tmall_category"});
			}else if(url.indexOf(".tmall.com/shop/view_shop.htm")!=-1){ //天猫旗舰全部展示
				selectorElementList.push({"element":"dl.item", "page":"tmall_shop_01"});
			}else if(url.indexOf(".tmall.com/search.htm")!=-1){
				selectorElementList.push({"element":"dl.item", "page":"tmall_shop_search"});
			}
			else if(url.indexOf("//maiyao.liangxinyao.com/shop/view_shop.htm")!=-1 
				|| url.indexOf("//maiyao.liangxinyao.com/search.htm")!=-1
				|| url.indexOf("//maiyao.liangxinyao.com/category")!=-1){ //阿里大药房
				selectorElementList.push({"element":"#J_ShopSearchResult dl.item", "page":"ali_yao"});
			}
			else if (url.indexOf("//search.jd.com/Search")!=-1 || url.indexOf("//search.jd.com/search")!=-1) { //京东搜索
				selectorElementList.push({"element":"#J_goodsList li.gl-item", "page":"jd_search"});
			}else if(url.indexOf("//search.jd.hk/Search")!=-1){ //京东国际https://search.jd.hk/Search?keyword=ds
				selectorElementList.push({"element":"#plist li.gl-item", "page":"jd_hk_search"}); //可能已弃用
				selectorElementList.push({"element":"li[class^='productList_']", "page":"jd_hk_new_search"});//20240119添加
			}else if(url.indexOf("//mall.jd.com/view_search")!=-1){ //京东大药房
				selectorElementList.push({"element":"li.jSubObject", "page":"jd_yao_search"});
			}else if(url.indexOf("www.jd.com")!=-1 && window.location.pathname=="/"){
				selectorElementList.push({"element":"#feedContent0 li.more2_item_good", "page":"jd_homepage"});
			}
			else if(url.indexOf("category.vip.com/suggest.php")!=-1){
				selectorElementList.push({"element":"#J_searchCatList .c-goods-item", "page":"vpinhui_search_page"});
			}else if(url.indexOf("://list.vip.com")!=-1){
				selectorElementList.push({"element":"#J_wrap_pro_add .c-goods-item", "page":"vpinhui_list"});
			}
			return selectorElementList;
		},
		transformElements:function(selectors){
			const items = [];
			selectors.forEach((elementObj)=>{
				if(elementObj.element){
					let page = elementObj.page;
					let elements = document.querySelectorAll(elementObj.element);
					elements.forEach((element)=>{
						if(element){
							items.push({"element":element, "page":page});
						}
					});
				}
			});
			this.queryAll(items);
		},
		queryAll:function(items){
			this.intervalCompleted = false;
			const promises = [];
			const histories = this.getHistories();
			items.forEach((item)=>{
				promises.push(this.queryOne(item,histories));
			});
			Promise.all(promises).then((result)=>{
				this.intervalCompleted = true;
			});
		},
		queryOne:function(item,histories){
			const { element, page } = item;
			const self = this;
			return new Promise(function(resolve, reject){
				if(element.getAttribute("chaowantianxia")){  //当存在时,说明已经处理过了
					resolve("processed");
					return;
				}
				element.setAttribute("chaowantianxia", "true");
				element.style.position = "relative";
				element.addEventListener("click", function(e){
					element.insertAdjacentHTML('beforeend', browsedHtml);
				});

				let goodsDetailUrl = null;				
				if(page==="taobao"){
					goodsDetailUrl = element.querySelector("a[id^='J_Itemlist_TLink_']").getAttribute("href");
				}else if(page==="taobao_new"){
					let href = element.getAttribute("href");
					goodsDetailUrl = href.indexOf("http") == -1 ? (location.protocol + href) : href;
				}else if(page==="taobao_category"){
					goodsDetailUrl = element.querySelector(".J_TGoldData").getAttribute("href");
				}else if(page==="taobao_shop_search"){
					goodsDetailUrl = element.querySelector(".J_TGoldData").getAttribute("href");
				}else if(page==="ali_yao"){
					goodsDetailUrl = element.querySelector(".J_TGoldData").getAttribute("href");
				}else if(page==="tmall_chaoshi_search"){
					let href = element.querySelector("div.product-img").querySelector("a").getAttribute("href");
					goodsDetailUrl = href.indexOf("http") ==-1 ? (location.protocol + href) : href;
				}else if(page==="tmall_chaoshi_search_new"){
					let href = !!element.getAttribute("href")?element.getAttribute("href"):element.querySelector("a").getAttribute("href");
					goodsDetailUrl = href.indexOf("http") ==-1 ? (location.protocol + href) : href;
				}else if(page==="tmall_hk"){
					goodsDetailUrl = element.querySelector("a.productImg").getAttribute("href");
				}else if(page==="tmall_category"){
					goodsDetailUrl = element.querySelector(".J_TGoldData").getAttribute("href");
				}else if(page==="tmall_shop_01"){
					goodsDetailUrl = element.querySelector("dt.photo").querySelector("a").getAttribute("href");
				}else if(page==="tmall_shop_search"){
					goodsDetailUrl = element.querySelector(".J_TGoldData").getAttribute("href");
				}else if(page=="tmall_chaoshi_search_20240115"){
						goodsDetailUrl = element.querySelector("a").getAttribute("href");
				}else if(page==="jd_search"){
					goodsDetailUrl = element.querySelector("div.p-img").querySelector("a").getAttribute("href");
				}else if(page==="jd_hk_search"){
					goodsDetailUrl = element.querySelector("div.p-img").querySelector("a").getAttribute("href");
				}else if(page==="jd_hk_new_search"){
					goodsDetailUrl = element.querySelector("a").getAttribute("href");
				}else if(page==="jd_yao_search"){
					goodsDetailUrl = element.querySelector("div.jPic").querySelector("a").getAttribute("href");
				}else if(page=="jd_homepage"){
					goodsDetailUrl = element.querySelector("a[class='more2_lk']").getAttribute("href");
				}
				else if(page=="vpinhui_search_page"){
					goodsDetailUrl = element.querySelector("a").getAttribute("href");
				}else if(page=="vpinhui_list"){
					goodsDetailUrl = element.querySelector("a").getAttribute("href");
				}
				if(!goodsDetailUrl){
					resolve("exception-url-null");
					return;
				}
				
				let analysisData = null;
				if(page.indexOf("jd_")!=-1){
					let jdId = obtainParameterBySuffix(goodsDetailUrl);
					if(!!jdId) analysisData = {"id":jdId, "platform":"jd"};
				}else if(page.indexOf("vpinhui_")!=-1){
					let vipId = obtainParameterBySuffix(goodsDetailUrl).replace("detail-","");;
					if(!!vipId){
						analysisData = {"id":vipId.split("-")[1], "platform":"vpinhui"};
					}
				}else{
					let platform = getPlatform(goodsDetailUrl);
					let id = getSearchParameter(goodsDetailUrl, "id");
					if(platform && id){
						analysisData = {"id":id, "platform":platform};
					}
				}
				if(!analysisData){
					resolve("exception-data-null");
					return;
				}
				
				if(histories.includes(analysisData.platform + "_" + analysisData.id)){
					element.insertAdjacentHTML('beforeend', browsedHtml);
				}
				
				const searchUrl = "https://t.jtm.pub/api/ebusiness/q/c?p="+analysisData.platform+"&id="+analysisData.id+"&no=3";
				makeRequest("GET", searchUrl, null).then((data)=>{
					if(data.code=="ok" && !!data.result){
						const {tip, encryptLink} = JSON.parse(data.result);
						if(tip){
							element.insertAdjacentHTML('beforeend', tip);
						}
						if(encryptLink){
							let decryptUrl = null;
							try{
								const decryptLink = atob(encryptLink);
								decryptUrl = decryptLink.split('').reverse().join('');
							}catch(e){}
							if(decryptUrl){
								self.relativeClickTo(page, element, decryptUrl);
							}
						}
					}
					resolve("success");
				});
			});
		},
		relativeClickTo:function(page, element, decryptUrl){
			const self = this;
			try{
				if(page.indexOf("jd_")!=-1){
					element.querySelectorAll("a").forEach((element_a)=>{
						if(element_a.getAttribute("href").indexOf("item.jd.com")!=-1){
							element_a.removeAttribute(onclick);
							element_a.addEventListener("click", function(e){
								e.preventDefault();
								e.stopPropagation();
								openInTab(decryptUrl);
							});
						}
					});
				}
				else if(page.indexOf("taobao_")!=-1 || page.indexOf("tmall_")!=-1){
					element.addEventListener("click", function(e){
						const target = e.target
						const tagName = target.tagName.toUpperCase();
						let isPreventDefault = false;
						if(tagName==="A"){ //只有点击A标签才去判断
							const href = target.getAttribute("href");
							const isDetail = [/detail\.tmall\.com\/item\.htm/, /item\.taobao\.com\/item\.htm/]
								.map((reg)=> reg.test(href))
								.some((result) => result);
							if(isDetail){
								isPreventDefault = true;
							}
						}else{
							isPreventDefault = true;
						}
						if(isPreventDefault){
							e.preventDefault();
							e.stopPropagation();
							openInTab(decryptUrl);
						}	
					});
				}
				else if(page.indexOf("vpinhui_")!=-1){
					element.querySelectorAll("a").forEach((element_a)=>{
						if(element_a.getAttribute("href").indexOf("detail.vip.com/detail-")!=-1){
							element_a.addEventListener("click", function(e){
								e.preventDefault();
								e.stopPropagation();
								openInTab(decryptUrl);
							});
						}
					});
				}
			}catch(e){
				console.log(e);
			}
		},
		start:function(){
			const selectors = this.pickupsoso();
			this.transformElements(selectors);
			
			if(this.intervalCompleted){
				this.transformElements(selectors);
			}
			setInterval(()=>{
				if(this.intervalCompleted){
					this.transformElements(selectors);
				}
			}, 1500);
		}
	};
	
	[CouponDiscoverer, CouponSearcher].forEach((fun)=>{
		fun.start();
	});
})();