-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathso-post-monitor.user.js
193 lines (173 loc) · 8.2 KB
/
so-post-monitor.user.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// ==UserScript==
// @name Stack Exchange post monitor
// @description a hacky bugfix of Stack Exchange questions not updating properly.
// @description currently fixes the posts not updating their deleted/undeleted status.
// @include http://*stackexchange.com/*
// @include http://*stackoverflow.com/*
// @include http://*mathoverflow.com/*
// @include http://*serverfault.com/*
// @include http://*superuser.com/*
// @include http://*stackapps.com/*
// @include http://*askubuntu.com/*
// @version 2.2
// ==/UserScript==
(function () {
if(!$.fn.addBack) $.fn.addBack = function(selector){
return this.andSelf().filter(selector);
};
/// core
function UpdateTask(target) {
this.target = $(target);
this.answerId = this.target.data("answerid");
this.questionId = this.target.data("questionid");
if (this.questionId) return;
if (/^\/questions\/\d+/.test(location.pathname)){
this.questionId = this.target.closest("#close-question-form")
.find(".question").data("questionid")
|| $("#question").data("questionid");
} else if (/^\/review\/(first-posts|late-answers|low-quality-posts)\/\d+/
.test(location.pathname)){
this.questionId = $(".question").data("questionid");
}
}
/// on new task
UpdateTask.prototype.addSelf = function () {
var thisTask = this,
questionIdPromise = $.Deferred();
updateTasks.push(this);
updateTasksById[this.answerId || this.questionId] = this;
if (this.questionId) {
questionIdPromise.resolve(this.questionId);
} else {
//todo: this is ugly as a sin, even for a fallback. Use [the API][1] instead
//[1]: http://api.stackexchange.com/docs/answers-by-ids
$.get("/posts/" + this.answerId + "/edit").then(function (response) {
questionIdPromise.resolve(
$(response).find("a.cancel-edit").attr("href").match(/questions\/(\d+)/)[1]
);
});
}
questionIdPromise.then(function (questionId) {
if (!updateTasksByQuestionId[questionId]) updateTasksByQuestionId[questionId] = [];
updateTasksByQuestionId[questionId].push(thisTask);
});
//deletion timer:
if(this.target.is(".question-page #question")){
$(document).on("ajaxComplete", function(_,_,ajaxOptions){
if(/\/flags\//.test(ajaxOptions.url) && ajaxOptions.type === "POST"){
if(timerAttached){
console.log("attaching twice (???)");
return;
}
timerAttached = true;
thisTask.target.find(".post-menu").append($("<span>", {"class": "lsep", text: "|"}))
.append($("<input>", {type: "checkbox", id: "monitor-post-deletion", change: function(){
timeDeletion = updateAlways = this.checked;
}})).append($("<span>", {text: " time this post deletion"}));
thisTask.creationTime = Math.min.apply(Math,
thisTask.target.find(".user-action-time span[title]").map(function(){
return Date.parse(this.title);
})
);
}
});
}
};
/// on clock tick
UpdateTask.updateAll = function () {
var visibilityState = document.visibilityState || document.webkitVisibilityState || "visible";
if (visibilityState === "visible" || updateAlways) {
for (var questionId in updateTasksByQuestionId) {
var updateTasksForQuestion = updateTasksByQuestionId[questionId];
if (updateTasksForQuestion.some(function(x){return x.target.is(":visible")})) {
$.get("/questions/" + questionId, {
dataType: "html"
}).then(function (questionId, updateTasksForQuestion, response) {
$response = $(response);
if($(".pager-answers a", $response).length){
console.log("paging not supported");
return;
}
updateTasksForQuestion.forEach(function (updateTask) {
var $targetInResponse;
if (updateTask.answerId) {
$targetInResponse = $("#answer-" + updateTask.answerId, $response);
} else {
$targetInResponse = $("#question", $response);
}
//yep, even deleted questions are `.deleted-answer`s.
if ($targetInResponse.is(":not(.deleted-answer)")) { //and exists
updateTask.target.removeClass("deleted-answer");
} else {
updateTask.target.addClass("deleted-answer");
// deletion times:
if(timeDeletion && $targetInResponse.is("#question")){
updateTask.deletionTime = Date.parse(
$(".question-status .relativetime", $response).attr("title")
);
updateTask.deletionTimeError = 500;
timeDeletion = false;
timerReport(updateTask);
}
}
});
}.bind(null, questionId, updateTasksForQuestion))
.fail(function (updateTasksForQuestion, jqxhr) {
if(jqxhr.status === 404){
updateTasksForQuestion.forEach(function(updateTask){
updateTask.target.addClass("deleted-answer");
if(timeDeletion && updateTask.target.is(".question-page #question")){
updateTask.deletionTime = Date.now() - UPDATE_TIMEOUT / 2;
updateTask.deletionTimeError = UPDATE_TIMEOUT/2;
timeDeletion = false;
timerReport(updateTask);
}
})
}else if (jqxhr.status > 0){
console.log(arguments);
}
}.bind(null,updateTasksForQuestion));
}
}
}
};
/// globals
var UPDATE_TARGET_SELECTOR = "div[data-answerid], div[data-questionid]",
UPDATE_TIMEOUT = 10000,
updateTasks = [],
updateTasksById = {},
updateTasksByQuestionId = {},
updateAlways = false,
timeDeletion = false,
timerAttached = false;
/// adding tasks
$(UPDATE_TARGET_SELECTOR).each(function () {
new UpdateTask(this).addSelf();
});
new MutationObserver(function (records) {
records.forEach(function (record) {
$(record.addedNodes)
.find(UPDATE_TARGET_SELECTOR).addBack(UPDATE_TARGET_SELECTOR)
.each(function () {
new UpdateTask(this).addSelf();
});
});
}).observe(document.body, {
childList: true,
subtree: true
});
setInterval(UpdateTask.updateAll, UPDATE_TIMEOUT);
$(document).on("visibilitychange", UpdateTask.updateAll);
/// timer functions
function timerReport(updateTask){
debugger;
$checkbox = $("#monitor-post-deletion");
var dt = updateTask.deletionTime - updateTask.creationTime;
var dte = updateTask.deletionTimeError;
$checkbox.next().text(
"deleted after " + (dt/1000).toFixed() + " +/- " + (dte/1000) + "s"
);
$checkbox.remove();
document.title = "deleted - " + document.title;
}
})();