// ==UserScript==
// @name Keylol Chinaplay Table
// @namespace https://greasyfork.org/users/34380
// @version 20231111
// @description 在购买心得 Chinaplay 板块的帖子一楼开始位置添加折扣表格,折后价和史低可排序。
// @match https://keylol.com/*
// @grant none
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
if (document.querySelector('.subforum_left_title_left_up').innerText.match(/Chinaplay/)) {
document.querySelector('head').insertAdjacentHTML('beforeend', ``);
var html_table = `
`;
var floor1 = document.querySelector('.t_f');
var anchor = floor1.querySelector('.t_f .pstatus') || floor1.querySelector('.original_text_style1');
if (anchor) {
anchor.insertAdjacentHTML('afterend', '' + html_table);
} else {
floor1.insertAdjacentHTML('afterbegin', html_table);
}
var tbody = document.querySelector('#table-chinaplay > tbody');
// table col > game discount hist region date code cart link
var game;
var discount = 0;
var hist = 0;
var region;
var date;
var code = '';
var link;
var trs = [];
var i_start = 0;
var i_end = 0;
var nodes = floor1.childNodes;
nodes.forEach((node) => {
node.childNodes.forEach((_node) => {
getContent(_node);
});
});
newTable();
document.querySelector('#table-chinaplay > thead > tr').addEventListener('click', function (event) {
const target = event.target;
if (target.nodeName == 'TD' && target.hasAttribute('data-reverse')) {
const col = target.getAttribute('data-col');
let reverse = target.getAttribute('data-reverse');
let sorted;
if (reverse == 0) {
sorted = Array.from(tbody.querySelectorAll('tr')).sort((a, b) => {
return b.querySelector('td[data-' + col + ']').getAttribute('data-' + col) - a.querySelector('td[data-' + col + ']').getAttribute('data-' + col);
});
const siblings = this.querySelectorAll('[data-reverse="1"]');
target.setAttribute('data-reverse', '1');
siblings.forEach((node) => { node.setAttribute('data-reverse', '0'); });
} else {
sorted = Array.from(tbody.querySelectorAll('tr')).sort((a, b) => { return a.querySelector('td[data-' + col + ']').getAttribute('data-' + col) - b.querySelector('td[data-' + col + ']').getAttribute('data-' + col); });
const siblings = this.querySelectorAll('[data-reverse="0"]');
target.setAttribute('data-reverse', '0');
siblings.forEach((node) => { node.setAttribute('data-reverse', '1'); });
}
sorted.forEach((node) => {
tbody.insertAdjacentElement('beforeend', node);
});
}
});
function newTable() {
var html = '';
trs.forEach((tds) => {
html = html + `${tds[0]} | ${tds[1]} | ${tds[2]} | ${tds[3]} | ${tds[4]} | ${tds[5]} | 加购 |
`;
});
tbody.innerHTML = html;
}
function getContent(node) {
if (node.nodeName == 'SPAN' || node.nodeName == 'STRONG') {
node.childNodes.forEach((_node) => {
getContent(_node);
});
} else if (node.nodeName == 'H1') {
var matched = node.innerText.match(/(\d+(\.\d+)?)(.*)((.*))/);
if (matched) {
date = matched[1];
region = matched[4];
i_end = trs.length;
for (; i_start < i_end; i_start++) {
trs[i_start][5] = code;
}
var matched2 = matched[3].match(/.*《(.*)》/);
if (matched2) {
game = matched2[1];
}
}
} else if (node.nodeName == 'A') {
if (node.href.match(/https:\/\/store\.steampowered\.com\/app\/\d+\//) || node.href.match(/https:\/\/store\.steampowered\.com\/sub\/\d+\//)) {
game = node.innerText;
link = node.href.match(/(.*\d+\/)/)[1];
} else if (node.href.match(/https:\/\/chinaplay\.store\/detail\/\S+\//)) {
var cart = node.href.match(/https:\/\/chinaplay\.store\/detail\/\S+\//)[0];
trs.push([game, discount, hist, region, date, code, cart, link]);
hist = 0;
}
} else if (node.nodeName == '#text') {
var content = node.textContent;
if (content.match(/史低:?(\d+(\.\d+)?)/)) {
hist = content.match(/史低:?(\d+(\.\d+)?)/)[1];
} else if (content.match(/(\d+(\.\d+)?)元/)) {
discount = content.match(/(\d+(\.\d+)?)元/)[1];
} else if (content.match(/折扣码:/)) {
code = node.parentNode.innerText.match(/折扣码:(\S+)/)[1];
}
}
}
}
})();