Last active
April 16, 2023 01:32
-
-
Save oglops/187962a6017e6d6fb021636a9eeea055 to your computer and use it in GitHub Desktop.
给天猫评论加入"有数字"的选项,老婆大人专用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name withDigit | |
// @namespace taobao | |
// @include https://detail.tmall.com/* | |
// @version 0.02 | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js | |
// @grant GM_xmlhttpRequest | |
// @grant GM_addStyle | |
// ==/UserScript== | |
GM_addStyle("table.crap td { padding:0 5px 0 5px; }"); | |
var gmailStrikeThroughButton = { | |
underlineButtons: null, | |
imageButtons: null, | |
page: 1, | |
pageRange:39, | |
// digit_label: null, | |
digit_checkbox: null, | |
activeButtonClassName: null, | |
canvas_frame_document: null, | |
text_editor: null, | |
compose_method: '', | |
button_exist: false, | |
digitHackInstalled: function(){ | |
var checkboxHack=document.evaluate('//input[@id="withDigit"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength; | |
return checkboxHack; | |
}, | |
set_rate_grid_visibility: function(visible) { | |
rate_grid_table = document.evaluate('//div[@class="rate-grid"]/table[1]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0); | |
if (visible==0){ | |
if (rate_grid_table != null) rate_grid_table.style.display = 'none'; | |
} | |
else{ | |
if (rate_grid_table != null) rate_grid_table.style.display = ''; | |
} | |
}, | |
set_page_button_visibility: function(visible){ | |
console.log("in set_page_button_visibility"); | |
var pageButtons = document.evaluate('//button[@class="digitsHackPageButton"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); | |
if (visible==0){ | |
for ( var i=0 ; i < pageButtons.snapshotLength; i++ ) | |
pageButtons.snapshotItem(i).style.display = 'none'; | |
} | |
else{ | |
for ( var i=0 ; i < pageButtons.snapshotLength; i++ ) | |
pageButtons.snapshotItem(i).style.display = ''; | |
} | |
console.log("in after set_page_button_visibility"); | |
}, | |
addButton: function() { | |
console.log("in addButton func"); | |
if (gmailStrikeThroughButton.digitHackInstalled()){ | |
console.log("The hack is already installed, skip."); | |
return; | |
} | |
var underlineButton = this.underlineButtons.snapshotItem(0); | |
var previousPageButton = this.canvas_frame_document.createElement('button'); | |
previousPageButton.innerHTML = '<<<'; | |
previousPageButton.setAttribute('class','digitsHackPageButton'); | |
previousPageButton.setAttribute('style', 'width:60px;'); | |
underlineButton.parentNode.insertBefore(previousPageButton, underlineButton); | |
nextPageButton = this.canvas_frame_document.createElement('button'); | |
nextPageButton.innerHTML = '>>>'; | |
nextPageButton.setAttribute('class','digitsHackPageButton'); | |
nextPageButton.setAttribute('style', 'width:60px;'); | |
underlineButton.parentNode.insertBefore(nextPageButton, underlineButton); | |
// create checkbox | |
var checkbox = document.createElement('input'); | |
checkbox.type = "checkbox"; | |
checkbox.name = "digitsHackCheckbox"; | |
checkbox.value = "value"; | |
checkbox.id = "withDigit"; | |
var newlabel = document.createElement("Label"); | |
// newlabel.setAttribute("for",id_from_input); | |
newlabel.innerHTML = " Digits "; | |
underlineButton.parentNode.insertBefore(checkbox, underlineButton); | |
underlineButton.parentNode.insertBefore(newlabel, underlineButton); | |
// gmailStrikeThroughButton.pageButtons=[previousPageButton,nextPageButton]; | |
// hide page buttons initially | |
console.log("before set_page_button_visibility"); | |
gmailStrikeThroughButton.set_page_button_visibility(0); | |
// previous and next page at the bottom | |
var bottom_previous_button = previousPageButton.cloneNode(true); | |
var bottom_next_button = nextPageButton.cloneNode(true); | |
var last_page_button = document.evaluate('//span[@class="rate-page-prev"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0); | |
last_page_button.parentNode.insertBefore(bottom_previous_button, last_page_button); | |
last_page_button.parentNode.insertBefore(bottom_next_button, last_page_button); | |
// checkbox events | |
// http://jsfiddle.net/wewtnbpc/1/ | |
var _selector = document.querySelector('input[name=digitsHackCheckbox]'); | |
_selector.addEventListener('change', function (e) { | |
if (_selector.checked) { | |
// hide rate grid | |
gmailStrikeThroughButton.set_rate_grid_visibility(0); | |
gmailStrikeThroughButton.set_page_button_visibility(1); | |
gmailStrikeThroughButton.load_rate_info(0); | |
} else { | |
gmailStrikeThroughButton.set_rate_grid_visibility(1); | |
gmailStrikeThroughButton.set_page_button_visibility(0); | |
} | |
}); | |
// function call to retrieve rates | |
previousPageButton.addEventListener('click', function(e){gmailStrikeThroughButton.load_rate_info(-1);} , false); | |
nextPageButton.addEventListener('click', function(e){gmailStrikeThroughButton.load_rate_info(1);} , false); | |
bottom_previous_button.addEventListener('click', function(e){gmailStrikeThroughButton.load_rate_info(-1);} , false); | |
bottom_next_button.addEventListener('click', function(e){gmailStrikeThroughButton.load_rate_info(1);} , false); | |
}, | |
// http://www.hnldesign.nl/work/code/check-if-element-is-visible/ | |
isVisible: function (node) { | |
// Am I visible? | |
// Height and Width are not explicitly necessary in visibility detection, the bottom, right, top and left are the | |
// essential checks. If an image is 0x0, it is technically not visible, so it should not be marked as such. | |
// That is why either width or height have to be > 0. | |
var rect = node.getBoundingClientRect(); | |
return ( | |
(rect.height > 0 || rect.width > 0) && | |
rect.bottom >= 0 && | |
rect.right >= 0 && | |
rect.top <= (window.innerHeight || document.documentElement.clientHeight) && | |
rect.left <= (window.innerWidth || document.documentElement.clientWidth) | |
); | |
}, | |
load_rate_info: function(mode) { | |
var seller_id = gmailStrikeThroughButton.get_seller_id(/sellerId:\"\d+/g); | |
var item_id = gmailStrikeThroughButton.get_seller_id(/temId:\"\d+/g); | |
console.log("seller id:" + seller_id); | |
console.log("item id:" + item_id); | |
// if user clicks page button at the bottom, scroll to top bar | |
var checkbox = document.evaluate('//input[@id="withDigit"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0); | |
if ( gmailStrikeThroughButton.isVisible(checkbox)!=true){ | |
if (mode!=0){ | |
checkbox.scrollIntoView(); | |
window.scrollBy(0,-60); | |
} | |
} | |
var rate_grid = document.evaluate('//div[@class="rate-grid"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0); | |
old_table = document.evaluate('//table[@class="crap"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0); | |
if (old_table != null) old_table.remove(); | |
var current_page = gmailStrikeThroughButton.page; | |
var new_page = current_page + gmailStrikeThroughButton.pageRange*mode; | |
if (mode==0){ | |
// new_page=current_page + gmailStrikeThroughButton.pageRange*mode; | |
// if new page is smaller, swap new page and current page | |
console.log("mode == 0"); | |
if (new_page == current_page){ | |
new_page=current_page-1; | |
current_page = new_page-gmailStrikeThroughButton.pageRange; | |
console.log("in mode 0 "+current_page + "-" + new_page); | |
} | |
console.log("reload " + current_page + "-" + new_page); | |
} | |
else{ | |
// http://stackoverflow.com/questions/16201656/how-to-swap-two-variables-in-javascript | |
// new_page = [current_page, current_page = new_page][0]; | |
if (new_page<current_page){ | |
new_page-=1; | |
current_page=new_page- gmailStrikeThroughButton.pageRange; | |
current_page-=1; | |
new_page-=1; | |
console.log("in mode "+current_page + "-" + new_page); | |
// console.log("you are subtracting page"); | |
// [new_page,current_page] = [current_page,new_page]; | |
// console.log(current_page + "-" + new_page); | |
// gmailStrikeThroughButton.page = current_page + 1*mode; | |
} | |
// store new current page for next time | |
else{ | |
gmailStrikeThroughButton.page = new_page + 1*mode; | |
} | |
} | |
gmailStrikeThroughButton.page = new_page+1; | |
if ((current_page<=0)|| (new_page<=0)){ | |
console.log("current page is:"+ current_page); | |
current_page=1; | |
new_page = current_page + gmailStrikeThroughButton.pageRange; | |
gmailStrikeThroughButton.page = new_page+1; | |
} | |
console.log("finally "+ current_page + "-" + new_page); | |
var rateTable; | |
if (document.evaluate('//div[@class="rate-grid"]/table', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength <= 1) { | |
console.log('only 1 or 0 table found, insert new table'); | |
rateTable = document.createElement('table'); | |
rateTable.setAttribute('class', 'crap'); | |
if (rate_grid == null) table_parent = rate_grid; | |
else table_parent = rate_grid.parentNode; | |
table_parent.insertBefore(rateTable, rate_grid.nextSibling); | |
var header = rateTable.createTHead(); | |
var row = header.insertRow(0); | |
row.style.borderBottom = "1px dashed gray"; | |
var headers = ["rate " + current_page + "-" + new_page, "variation", "date"]; | |
headers.forEach(function(r, i) { | |
var cell = row.insertCell(); | |
cell.innerHTML = r; | |
// if (i==2) | |
// cell.style.width = "60px"; | |
if (i == 1) cell.style.width = "180px"; | |
}); | |
} | |
var pages = Array.from(range(current_page, new_page)); | |
pages.forEach(function(p) { | |
var ret = (function(p) { | |
var url = "https://rate.tmall.com/list_detail_rate.htm?itemId=" + item_id + "&sellerId=" + seller_id + "¤tPage=" + p; | |
// console.log(url); | |
GM_xmlhttpRequest({ | |
method: "GET", | |
headers: { | |
"Accept": "application/json" | |
}, | |
url: url, | |
onload: function(res) { | |
var myJSON = res.response; | |
var myRe = /"auctionSku":"([^"]*)"((?!rateContent).)*"rateContent":"([^"]*)","rateDate":"([^"]*)"/g; | |
var str = myJSON; | |
var myArray; | |
while ((myArray = myRe.exec(str)) !== null) { | |
color = myArray[1]; | |
rate_content = myArray[3].replace(/&((?!;).)*;/g, ""); | |
date = myArray[4].split(" ")[0]; | |
if (/.*\d+.*/g.test(rate_content)) { | |
// var msg = 'Found ' + rate_content + ' = ' + color + ' - ' + date; | |
// console.log(msg); | |
var row = rateTable.insertRow(); | |
row.style.borderBottom = "1px dashed gray"; | |
var cell1 = row.insertCell(); | |
var cell2 = row.insertCell(); | |
var cell3 = row.insertCell(); | |
cell1.innerHTML = rate_content; | |
cell2.innerHTML = color.replace(/;/g, "<br>"); | |
cell3.innerHTML = date; | |
} // if found digits in rate | |
} // while there are still new rate | |
} // GM_xmlhttpRequest onload | |
}); //GM_xmlhttpRequest | |
}); // define closure | |
ret(p); // call closure | |
}); // pages.forEach | |
}, | |
get_seller_id: function(expression) { | |
// alert(" getting seller id"); | |
// var seller_id = gmailStrikeThroughButton.canvas_frame_document.head.innerHTML.match(/sellerId:\"\d+/g); | |
var seller_id = gmailStrikeThroughButton.canvas_frame_document.head.innerHTML.match(expression); | |
var myRe = /\d+/g; | |
var id = myRe.exec(seller_id[0])[0]; | |
// var id=/\d+/g.exec(seller_id[0])[0]; | |
// var id=10; | |
return id; | |
}, | |
detectComponents: function(e) { | |
var composing = false; | |
var fix_width = false; | |
var is_firefox = (navigator.userAgent.indexOf('Firefox') != -1); | |
var is_chrome = (navigator.userAgent.indexOf('Chrome') != -1); | |
if (e != null) { | |
// old compose | |
if (e.target.nodeName == 'IFRAME' && e.target.className.indexOf('editable') != -1) { | |
// console.log("old firefox!"); | |
gmailStrikeThroughButton.compose_method = 'old'; | |
composing = true; | |
gmailStrikeThroughButton.text_editor = e.target.contentDocument; | |
gmailStrikeThroughButton.canvas_frame_document = e.target.ownerDocument; | |
// new compose - firefox | |
} else if (e.target.nodeName == 'IFRAME' && e.target.parentNode.className.indexOf('editable') != -1 && is_firefox) { | |
// console.log("new firefox!"); | |
composing = true; | |
fix_width = true; | |
gmailStrikeThroughButton.compose_method = 'new_firefox'; | |
gmailStrikeThroughButton.text_editor = e.target.contentDocument; | |
gmailStrikeThroughButton.canvas_frame_document = e.target.ownerDocument; | |
// new compose - chrome | |
} else if (e.target.innerHTML && e.target.innerHTML.indexOf('rate-filter') != -1 && | |
/*e.target.innerHTML.indexOf('+justifyLeft')==-1 */ | |
true && is_chrome) { | |
// console.log("new_chrome 1"); | |
composing = true; | |
fix_width = true; | |
gmailStrikeThroughButton.compose_method = 'new_chrome'; | |
gmailStrikeThroughButton.text_editor = e.target.ownerDocument; | |
gmailStrikeThroughButton.canvas_frame_document = e.target.ownerDocument; | |
} | |
} else if (e == null) { | |
// console.log("new_chrome 2"); | |
composing = true; | |
gmailStrikeThroughButton.compose_method = 'new_chrome'; | |
gmailStrikeThroughButton.text_editor = document; | |
gmailStrikeThroughButton.canvas_frame_document = document; | |
} | |
if (composing) { | |
var xPathResult = gmailStrikeThroughButton.canvas_frame_document.evaluate("//*[@class='rate-sort ' or @class='rate-sort']", gmailStrikeThroughButton.canvas_frame_document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); | |
if (gmailStrikeThroughButton.underlineButtons = xPathResult) { | |
console.log("add the shit"); | |
gmailStrikeThroughButton.addButton(); | |
} | |
} | |
}, | |
init: function() { | |
checkExist("//span[@class='rate-filter']", 1000, 10, gmailStrikeThroughButton.detectComponents); | |
} | |
}; | |
var range = function*(a, b) { | |
for (var i = a; i <= b; ++i) yield i; | |
}; | |
var rate_click_insert = function() { | |
console.log("after checkExists"); | |
var reviews_xPathResult = document.evaluate("//ul[@id='J_TabBar']/li[2]/a", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); | |
console.log("reviews_xPathResult: " + reviews_xPathResult.snapshotLength); | |
var review_button = reviews_xPathResult.snapshotItem(0); | |
// alert("Ready! change" + review_button); | |
review_button.addEventListener('click', function() { | |
console.log("in click listener"); | |
// alert("click!"); | |
// setTimeout(function(){ | |
// gmailStrikeThroughButton.init(); | |
// },1000); | |
checkExist("//*[@class='rate-sort ' or @class='rate-sort']", 1000, 10, gmailStrikeThroughButton.init); | |
}, false); | |
}; | |
var checkExist = function(xpath, time, timeout, delayed_func) { | |
var count = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength; | |
if (count >= 1) { | |
// alert("The element is displayed, you can put your code instead of this alert.") | |
console.log("found xpath:" + xpath); | |
delayed_func(); | |
return; | |
} else { | |
console.log("not found, current timeout:" + timeout); | |
if (timeout <= 0) return; | |
setTimeout(function() { | |
checkExist(xpath, time, --timeout, delayed_func); | |
}, time); | |
} | |
}; | |
document.onreadystatechange = function() { | |
// alert("Ready! change"); | |
// console.log(document.readyState); | |
if (document.readyState == "complete") { | |
// alert("Ready!"); | |
// gmailStrikeThroughButton.init(); | |
// run code when user clicks on href="#J_Reviews" | |
// var review_button = document.getElementsByClassName("tm-indcon")[1]; | |
// var reviews_xPathResult = gmailStrikeThroughButton.canvas_frame_document.evaluate("//a[@href='#J_Reviews']/parent::li", gmailStrikeThroughButton.canvas_frame_document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); | |
checkExist("//ul[@id='J_TabBar']/li[2]/a", 1000, 10, rate_click_insert); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment