forked from FiftyNine/scpper-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikiForum.js
141 lines (133 loc) · 5.42 KB
/
wikiForum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Process post's content, linkify text nodes and add linked numbers
function scpProcessForumPostContent(node, linkedNumbers, template, strict) {
if ((node.childNodes.length == 0) && (node.nodeType == Node.TEXT_NODE))
scpLinkifyTextNode(node, linkedNumbers, template, strict)
// Skip certain nodes
else if (node.nodeName.toUpperCase()!='A')
for (var i=0; i<node.childNodes.length; i++)
scpProcessForumPostContent(node.childNodes[i], linkedNumbers, template, strict);
else
scpAddLinkedNumber(node, linkedNumbers);
}
// Iterate through all posts on a page and linkify them
function scpForumProcessPosts() {
if (!scpperSettings.useLinkifier)
return;
var strict = false;
if (scpperSettings.linkifierTemplate == "strict")
strict = true;
var linkedNumbers = {};
var posts = document.querySelectorAll("#thread-container .content[id^=post-content-]");
for (var i=0; i<posts.length; i++)
if (/^post-content-\d+$/i.test(posts[i].id)) {
for (var j=scpWebsite.articleTemplates.length-1; j>=0; j--)
scpProcessForumPostContent(posts[i], linkedNumbers, scpWebsite.articleTemplates[j], strict);
}
}
// Override handlers on active page switch to use my handlers instead of default
function scpForumOverridePageHandlers(){
var pagers = document.querySelectorAll("div#thread-container-posts div.pager");
if (pagers==null) return;
for (var i=0; i<pagers.length; i++) {
var pageLinks = pagers[i].getElementsByClassName("target");
for (var j=0; j<pageLinks.length; j++) {
var linkElem = pageLinks[j].firstElementChild;
if (linkElem.nodeName.toUpperCase()=="A") {
var onclickText = linkElem.getAttribute("onclick");
var clickNumber = /\(\d+\)/.exec(onclickText);
if (clickNumber != null) {
onclickText = "scpperForumChangePageHandler("+clickNumber[0].slice(1, clickNumber[0].length-1)+")";
overrideElementHandler(linkElem, "pageLink_id_"+i+"_"+j, "onclick", onclickText);
}
}
}
}
}
//Runs after coming to a new page, set anchor by the first post of the page
function setForumPostAnchor() {
if (!scpperSettings.overrideForum)
return;
var currentPage = document.querySelector("#thread-container-posts .pager span.current");
if (currentPage && (currentPage.innerText != "1")) {
var firstPost = document.querySelector("#thread-container-posts .post[id^=post-]");
if (firstPost && /^post-\d+$/.test(firstPost.id)) {
var postId = firstPost.id;
// Do that so it won't scroll to the post
firstPost.removeAttribute("id");
try {
location.hash = "#"+postId;
} finally {
firstPost.id = postId;
}
}
} else {
// Prevent scrolling by storing the page's current scroll offset
var scrollV = document.body.scrollTop;
var scrollH = document.body.scrollLeft;
try {
location.hash = '';
} finally {
// Restore the scroll offset, should be flicker free
document.body.scrollTop = scrollV;
document.body.scrollLeft = scrollH;
}
}
}
// After anchor (hash part of location) changed loads specified post
function gotoAnchoredForumPage() {
if (!checkIfForum() || !scpperSettings.overrideForum)
return;
var postId = location.hash.replace("#", "");
if (/^#?post-\d+$/.test(postId)) {
var post = document.getElementById(postId);
if (!post) {
injectScript("scpperForumLoadPostOverride("+postId.replace("post-", "")+");");
}
}
else {
var currentPage = document.querySelector("#thread-container-posts .pager span.current");
if (currentPage && (currentPage.innerText != "1"))
injectScript("scpperForumChangePageHandler(1);");
}
}
// In case we came here by a link with specific post id we have to wait until correspondent forum page is loaded
// Otherwise go to the first page and process it
function intervalProcessForumPage() {
var postId = location.hash.replace("#", "");
var proc = function() {
scpForumProcessPosts();
scpForumOverridePageHandlers();
overrideUserInfoLinks();
enhanceLinks();
};
// Just in case. Kill interval after 10 iterations
var killCounter = 0;
if (/^post-\d+$/.test(postId)) {
var interval = setInterval(function() {
var post = document.getElementById(postId);
if (post || (killCounter >= 10)) {
proc();
clearInterval(interval);
}
killCounter++;
}, 1000)
}
else
proc();
}
// Process a forum page upon loading (runs only once)
function processForumPage() {
intervalProcessForumPage();
if (scpperSettings.overrideForum)
window.addEventListener("hashchange", gotoAnchoredForumPage);
}
// Listen to messages from forum page
document.addEventListener('ScpperExternalMessage', function(event) {
if (event.detail.text == "FORUM_POSTS_UPDATED_EXTERNAL") {
scpForumProcessPosts();
scpForumOverridePageHandlers();
overrideUserInfoLinks();
enhanceLinks();
setForumPostAnchor();
}
});