论坛改默认排序为发帖时间
// ==UserScript==
// @name 论坛改默认排序为发帖时间
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 支持油猴中文网和吾爱改默认排序为发帖时间
// @author hua
// @match https://bbs.tampermonkey.net.cn/forum-*-*.html
// @match https://www.52pojie.cn/forum-*-*.html
// @grant unsafeWindow
// @run-at document-start
// ==/UserScript==
changed_orderby = false
changed_thread = false
href = location.href
let public_time_selector_express
if (href.indexOf('https://www.52pojie.cn') === 0) {
changed_orderby = true
public_time_selector_express = 'td:nth-child(3) > em > span'
}
if (href.indexOf('https://bbs.tampermonkey.net.cn') === 0) {
public_time_selector_express = 'td > div > p > em:nth-child(3) > span'
}
let observer = new MutationObserver(function (mutationsList) {
for (let mutation of mutationsList) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
if (changed_orderby && changed_thread) {
observer.disconnect()
}
if (!changed_orderby) {
let orderby_node = document.querySelector('#filter_orderby > span')
if (orderby_node) {
changed_orderby = true
orderby_node.textContent = '发帖时间'
}
}
if (!changed_thread) {
let normal_thread_nodes = document.querySelectorAll('[id^="normalthread_"]')
console.log(normal_thread_nodes.length);
if (normal_thread_nodes.length > 0) {
changed_thread = sort_thread_node()
}
}
}
}
});
observer.observe(document, { childList: true, subtree: true });
function sort_thread_node() {
let table_node = document.querySelector('#threadlisttableid')
let thread_node_list = Array.from(table_node.children)
let sorted_node_list = []
for (let node of thread_node_list) {
let id = node.getAttribute('id')
if (id && id.indexOf('normalthread') === 0) {
break
}
sorted_node_list.push(node)
}
if (sorted_node_list.length > 0) {
// 判断最后一个节点是否渲染完成
if (!thread_node_list[thread_node_list.length - 1].querySelector(public_time_selector_express)) return false
thread_node_list.splice(0, sorted_node_list.length)
}
thread_node_list.sort(function (node_a, node_b) {
if (!node_a.querySelector(public_time_selector_express)) debugger
let public_str_time_a = node_a.querySelector(public_time_selector_express).textContent
let public_str_time_b = node_b.querySelector(public_time_selector_express).textContent
return new Date(public_str_time_b) - new Date(public_str_time_a)
});
Array.prototype.push.apply(sorted_node_list, thread_node_list)
table_node.innerHTML = ""
sorted_node_list.forEach(function (node) {
table_node.append(node)
})
return true
}