forked from FiftyNine/scpper-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscpEventOverrides.js
150 lines (138 loc) · 5.76 KB
/
scpEventOverrides.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
142
143
144
145
146
147
148
149
150
/*
This module contains functions that are used by SCPper Chrome extension to override default behaviour on SCP wiki and its regional branches
*/
function sendMessageToScpper(message) {
var messageEvent = new CustomEvent('ScpperExternalMessage', {detail: message});
document.dispatchEvent(messageEvent);
}
// Default wrapper for overriden functions
function scpperDefaultHandlerOverride(listener, setCallback, message, args) {
var oldCallback = setCallback(function (a) {
// Check for errors
if(!WIKIDOT.utils.handleError(a))
return;
try {
// Call original callback
oldCallback(a);
} finally {
// Send message home
sendMessageToScpper(message);
}
});
try {
listener.apply(null, args);
} finally {
setCallback(oldCallback);
}
}
// Override for forum page clicks
function scpperForumChangePageHandler(a) {
// Unfortunately original handler uses anonymous inline function for a callback,
// so we're gonna have to override it completely
OZONE.ajax.requestModule(
"forum/ForumViewThreadPostsModule",
{
pageNo:a,
t:WIKIDOT.forumThreadId,
order:($j("#thread-container").hasClass("reverse"))?"reverse":""
},
function(b){
if (!WIKIDOT.utils.handleError(b))
return;
$j("#thread-container-posts").html(b.body);
sendMessageToScpper({text: "FORUM_POSTS_UPDATED_EXTERNAL"});
OZONE.visuals.scrollTo("thread-container");
}
);
}
// Override for user info dialog click
function scpperUserInfoDialog(userId, aUserName) {
scpperDefaultHandlerOverride(WIKIDOT.page.listeners.userInfo, function(f) {
var res = WIKIDOT.page.callbacks.userInfo;
WIKIDOT.page.callbacks.userInfo = f;
return res;},
{text: "USER_INFO_DIALOG_EXTERNAL", userName: aUserName, userId: userId},
[userId]);
}
// Override for history button click
function scpperHistoryButtonClick() {
scpperDefaultHandlerOverride(WIKIDOT.page.listeners.historyClick, function(f) {
var res = WIKIDOT.page.callbacks.historyClick;
WIKIDOT.page.callbacks.historyClick = f;
return res;},
{text: "HISTORY_MODULE_LOADED_EXTERNAL"});
}
// Override for UpdateList button in history module
function scpperHistoryModuleUpdateListButtonClick() {
scpperDefaultHandlerOverride(WIKIDOT.modules.PageHistoryModule.listeners.updateList, function(f) {
var res = WIKIDOT.modules.PageHistoryModule.callbacks.updateList;
WIKIDOT.modules.PageHistoryModule.callbacks.updateList = f;
return res;},
{text: "ACTION_AREA_UPDATED_EXTERNAL"});
}
// Override for pages of revision list on historyModule
function scpperHistoryModuleUpdatePagedList(a) {
scpperDefaultHandlerOverride(updatePagedList, function(f) {
var res = WIKIDOT.modules.PageHistoryModule.callbacks.updatePageList;
WIKIDOT.modules.PageHistoryModule.callbacks.updatePageList = f;
return res;},
{text: "ACTION_AREA_UPDATED_EXTERNAL"},
[a]);
}
// Override for pagerate button click
function scpperPageRateButtonClick() {
scpperDefaultHandlerOverride(WIKIDOT.page.listeners.pageRate, function(f) {
var res = WIKIDOT.page.callbacks.pageRate;
WIKIDOT.page.callbacks.pageRate = f;
return res;},
{text: "PAGERATE_MODULE_LOADED_EXTERNAL"});
}
// Override for show people who rated this page button
function scpperShowVotersButtonClick(a, b) {
// Apparently original function doesn't use arguments, but still
scpperDefaultHandlerOverride(WIKIDOT.modules.PageRateModule.listeners.showWho, function(f) {
var res = WIKIDOT.modules.PageRateModule.callbacks.showWho;
WIKIDOT.modules.PageRateModule.callbacks.showWho = f;
return res;},
{text: "ACTION_AREA_UPDATED_EXTERNAL"},
[a, b]);
}
// Override for forum loading page for a specific post
function scpperForumLoadPostOverride(post_id) {
OZONE.ajax.requestModule(
"forum/ForumViewThreadPostsModule",
{ postId: post_id,
t: WIKIDOT.forumThreadId,
order: ($j("#thread-container").hasClass("reverse")) ? "reverse" : ""
},
function(c) {
$j("#thread-container-posts").show();
if (c.status == "no_post" || !WIKIDOT.utils.handleError(c)) {
return
}
$j("#thread-container-posts").html(c.body);
sendMessageToScpper({text: "FORUM_POSTS_UPDATED_EXTERNAL"});
OZONE.visuals.scrollTo("thread-container-posts");
},
null,
{ignoreCodeZero: true}
);
}
// Override a button interacting with action area
function overrideWikiBottomButton(id, onclick) {
// This button is handled somewhere deep in the wikidot core, so we just swap it with our own button and our own handler
var button = document.getElementById(id);
if (!button || (button.style.display == "none"))
return;
var myButton = button.cloneNode(true);
myButton.id = "scpper-"+id;
button.parentElement.insertBefore(myButton, button);
button.style.display = "none";
myButton.setAttribute("onclick", onclick);
// button.setAttribute("onclick", onclick);
var tip = document.getElementById(myButton.id+"hovertip");
if (tip)
OZONE.dialog.hovertip.makeTip(myButton, {context: tip, delay: 700, valign: "center"});
}
overrideWikiBottomButton("history-button", "scpperHistoryButtonClick();");
overrideWikiBottomButton("pagerate-button", "scpperPageRateButtonClick();");