forked from erinnichols/NewLove-chrome
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplansplus.user.js
322 lines (298 loc) · 14.9 KB
/
plansplus.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Revision History
// 1.0 - Initial release
// 1.0.1 - Added a new keyboard shortcut: "m" goes to the plan at the top of the autoread list
// 1.0.2 - Added keyboard shortcuts for numberpad keys as well as regular number keys
// 1.0.3 - Added autofinger polling and notifications to tab title -- [nichols]
// 1.1 - Added AJAX-updated autofinger list
// 1.1.1 - Stop newlove if user is not logged in
// 1.1.2 - Stop keyboard shortcuts when focused on a password field
// 2.0.0 - Update to Manifest v3, add timestamps to planlove
// Thanks to [youngian] and [nichols] for all their work on the original Newlove script!
// ==UserScript==
// @name PlansPlus
// @namespace http://www.grinnellplans.com
// @description Enhancements to GrinnellPlans: Newlove, keybord navigation, new windows for external links, and an updating autofinger list
// @version 1.1.2
// @include http://grinnellplans.com/*
// @include http://www.grinnellplans.com/*
// @match http://grinnellplans.com/*
// @match http://www.grinnellplans.com/*
// @include https://grinnellplans.com/*
// @include https://www.grinnellplans.com/*
// @match https://grinnellplans.com/*
// @match https://www.grinnellplans.com/*
// ==/UserScript==
function plansPlus () {
// **********************
// Reusable functions ---
// **********************
function showNotification (notificationText) {
$('#plansPlusNotification').clearQueue().remove();
var notificationContainer = $('<div id="plansPlusNotification"><div id="plansPlusNotificationClose">X</div></div>');
notificationContainer.append(notificationText).prependTo('body').show().animate({top: '+=41'}, 1000).delay(10000).animate({top: '-=41'}, 1000, function () {notificationContainer.remove();});
$('#plansPlusNotificationClose').bind('click', function() {notificationContainer.clearQueue().animate({top: '-=41'}, 1000, function() {notificationContainer.remove()});});
}
// **********************
// Get all preferences --
// **********************
var linkTarget = window.localStorage.getItem('linkTarget');
var quickLoveUser = window.localStorage.getItem('plansPlusUser');
var notificationSide = window.localStorage.getItem("notification") || 'left';
var notificationLevel = window.localStorage.getItem("notificationLevel") || "3";
var currentPathname = window.location.pathname;
// **********************
// Keyboard navigation --
// **********************
window.localStorage.setItem('inputFocused', 'false');
$('textarea, input:text, input:password').live('focus', function() {
window.localStorage.setItem('inputFocused', 'true');
}).blur(function() {
window.localStorage.setItem('inputFocused', 'false');
});
$(document.documentElement).keyup(function (event) {
var inputFocused = window.localStorage.getItem('inputFocused');
if (inputFocused == 'false' && $('input[value="Guest"]').length !== 1) {
if (event.which == 49 || event.which == 97) {
window.location = 'setpriv.php?myprivl=1';
} else if (event.which == 50 || event.which == 98) {
window.location = 'setpriv.php?myprivl=2';
} else if (event.which == 51 || event.which == 99) {
window.location = 'setpriv.php?myprivl=3';
} else if (event.which == 78) {
if ($('#autoread').length === 1) {
var nextPlan = $('.autoreadentry.last a').attr('href');
}
else if ($('table.mainpanel').length > 0) {
var nextPlan = $('table.mainpanel a[href ^= "read.php"]:last').attr('href');
}
if (nextPlan) {
window.location = nextPlan;
}
} else if (event.which == 77) {
if ($('#autoread').length === 1) {
var topPlan = $('.autoreadentry.first a').attr('href');
}
else if ($('table.mainpanel').length > 0) {
var topPlan = $('table.mainpanel a[href ^= "read.php"]:first').attr('href');
}
if (topPlan) {
window.location = topPlan;
}
} else if (event.which == 81) {
window.location = 'quicklove.php';
}
}
});
// **********************
// External links -------
// **********************
if (linkTarget === null) {
window.localStorage.setItem('linkTarget', '_blank');
var linkTarget = window.localStorage.getItem('linkTarget');
}
$('a[href ^= "http"]').attr('target', linkTarget);
// **********************
// Newlove ------------
// **********************
$('head').prepend('<style>.oldLove .result_sublist {display: none;} #plansPlusNotification {display: none; background: #F1EFC2; position: fixed; text-align: center; top: -41px; width: 100%; z-index: 1000; border-bottom: 1px solid #999; opacity: 0.9; line-height: 40px; height: 40px;} #plansPlusNotificationClose {color: #444444; font-family: Verdana, sans-serif; font-weight: bold; height: 40px; line-height: 40px; position: absolute; right: 5px; top: 0; cursor: pointer;} #plansPlusPreferences div {margin: 0 0 5px 0;}</style>');
if (currentPathname === '/search.php' && $('a[href="edit.php"]').length > 0) {
var currentSearch = window.location.search;
var currentUserStartIndex = currentSearch.indexOf("mysearch=") + 9;
var currentUserEndIndex = currentSearch.indexOf("&", currentUserStartIndex);
var currentSearchUser = currentSearch.substring(currentUserStartIndex, currentUserEndIndex);
if (quickLoveUser === null) {
window.localStorage.setItem('plansPlusUser', currentSearchUser);
showNotification('<strong>Now watching for new love for [' + currentSearchUser + '].</strong> You can change the user on <a href="customize.php">the preferences page</a>.');
var quickLoveUser = window.localStorage.getItem('plansPlusUser');
}
if (quickLoveUser === currentSearchUser) {
if (window.localStorage.getItem('prefsRecentlyChanged') === 'user') {
showNotification('<strong>You recently started watching for new love for [' + quickLoveUser + '].</strong> If it isn’t already, love will be filtered the next time you visit this page.');
window.localStorage.removeItem('prefsRecentlyChanged');
}
var currentLove = {};
var oldLove = JSON.parse(window.localStorage.getItem('oldLove'));
if (oldLove === null) {
var oldLove = {};
}
var newLoveCount = 0;
$('#search_results>li').each(function () {
var lover = $(this).find('a.planlove').text();
var loving = $(this).find('ul.result_sublist').text();
var oldLoving = oldLove[lover];
if(loving === oldLoving) {
$(this).addClass('oldLove');
}
else {
$(this).addClass('newLove');
newLoveCount++;
}
currentLove[lover] = loving;
});
if(newLoveCount > 0) {
if(newLoveCount == 1) {
showNotification('<strong>' + newLoveCount + ' new person has planloved you!</strong>');
} else {
showNotification('<strong>' + newLoveCount + ' new people have planloved you!</strong>');
}
}
window.localStorage.setItem('oldLove', JSON.stringify(currentLove));
}
}
// **********************
// Preferences ----------
// **********************
if (currentPathname === '/customize.php') {
var plansPlusPreferences = $('\
<div id="plansPlusPreferences">\
<h1 class="heading">PlansPlus Preferences</h1>\
<form action="#"><p>PlansPlus is tracking newlove for <input id="plansPlusUserInput" type="text" value="' + quickLoveUser + '" /> and opening links in <select id="plansPlusLinkTargetSelect"><option value="_blank">a new tab or window</option><option value="_self">the same tab or window</option></select>. Don’t like that? Change a preference and hit the update button, and viola! And remember, <strong>1, 2, 3</strong> = autoread level, <strong>n</strong> = next plan (the bottom one) in autoread, <strong>m</strong> = most recent plan (the top one) in autoread, <strong>q</strong> = quicklove.</p>\
<h3>Unread plan counts in the tab title:</h3>\
<input id="notify3" type="radio" name="_notifylevel" value="3" checked="checked"/> Levels 1 + 2 + 3 \
<input id="notify2" type="radio" name="_notifylevel" value="2"/> Levels 1 + 2 only \
<input id="notify1" type="radio" name="_notifylevel" value="1"/> Level 1 only \
<input id="notify0" type="radio" name="_notifylevel" value="0"/> Turn off notifications \
<br/>Show notifications on the: \
<input id="notificationLeft" type="radio" name="_notification" value="left" checked="checked"/> left \
<input id="notificationRight" type="radio" name="_notification" value="right"/> right (relative to the page title)<br/>\
<input id="plansPlusUpdateButton" type="submit" value="Update PlansPlus Preferences" /></form>\
</div>\
');
$('#preflist').after(plansPlusPreferences);
$('#plansPlusLinkTargetSelect option[value="' + linkTarget + '"]').attr('selected', 'selected');
if(notificationSide == 'right') {
$('#notificationRight').attr('checked', 'checked');
}
$('#notify' + notificationLevel).attr('checked', 'checked');
$('#plansPlusUpdateButton').bind('click', function(event) {
event.preventDefault();
window.localStorage.setItem('plansPlusUser', $('#plansPlusUserInput').val());
window.localStorage.setItem('linkTarget', $('#plansPlusLinkTargetSelect option:selected').val());
window.localStorage.setItem('inputFocused', 'false');
window.localStorage.setItem('notification', $('input[name="_notification"]:checked').val());
window.localStorage.setItem('notificationLevel', $('input[name="_notifylevel"]:checked').val());
if(quickLoveUser !== window.localStorage.getItem('plansPlusUser')) {
window.localStorage.setItem('prefsRecentlyChanged', 'user');
}
showNotification('<strong>PlansPlus preferences have been updated.</strong>');
});
}
// *********************************
// Refresh Autofinger List ---------
// *********************************
// From http://stackoverflow.com/questions/1187518/javascript-array-difference
Array.prototype.diff = function(a) {
return this.filter(function(i) {return !(a.indexOf(i) > -1);});
};
function refreshAutofingerList (freshAutofingerList, autofingerLevel, unreadCount) {
var currentAutofingerList = [];
// If the user is using the newer, table-less interface
if ($('#set_autoreadlev1').length === 1) {
$('#set_autoreadlev' + autofingerLevel).html('Level ' + autofingerLevel + ' <span class="unreadCount">(' + unreadCount + ')</span>');
if ($('#set_autoreadlev' + autofingerLevel).parent('.autoreadname').next('ul').length === 0) {
$('#set_autoreadlev' + autofingerLevel).parent('.autoreadname').after('<ul></ul>');
}
var $autofingerList = $('#set_autoreadlev' + autofingerLevel).parent('.autoreadname').next('ul');
$autofingerList.find('li a').each(function () {
currentAutofingerList.push($(this).text());
});
var newAutofingers = freshAutofingerList.diff(currentAutofingerList);
if (newAutofingers.length > 0) {
for (var j=0; j<newAutofingers.length; j++) {
$autofingerList.prepend('<li class="freshAutoreadentry autoreadentry" style="display: none;"><a href="read.php?searchname=' + newAutofingers[j] + '">' + newAutofingers[j] + '</a></li>');
}
$autofingerList.children('li').removeClass('even odd first last');
$autofingerList.children('li:even').addClass('even');
$autofingerList.children('li:odd').addClass('odd');
$autofingerList.children('li:first').addClass('first');
$autofingerList.children('li:last').addClass('last');
$autofingerList.children('li.freshAutoreadentry').fadeIn();
}
}
// Or if using the older, table interface
else {
$('p.imagelev3').css({'margin-left': 0, 'padding-right': 0});
$('td a[href="setpriv.php?myprivl=' + autofingerLevel + '"]').html('Level ' + autofingerLevel + ' <span class="unreadCount">(' + unreadCount + ')</span>');
var $currentAutoreadLevelRow = $('a[href*="mark_as_read"]').parent('td').parent('tr');
if ($currentAutoreadLevelRow.find('a.lev2').attr('href').replace('setpriv.php?myprivl=', '') == autofingerLevel) {
$('a.lev3').each(function () {
currentAutofingerList.push($(this).text().trim());
});
var newAutofingers = freshAutofingerList.diff(currentAutofingerList);
if (newAutofingers.length > 0) {
for (var j=0; j<newAutofingers.length; j++) {
$currentAutoreadLevelRow.after('<tr><td></td><td></td><td><p class="imagelev3" style="margin-left: 0px; padding-right: 0px;"> </p></td><td><a class="lev3" href="read.php?searchname=' + newAutofingers[j] + '">' + newAutofingers[j] + '</a></td></tr>');
}
}
}
}
}
var totalTime = 0;
var interval;
var intervalRules = [
[(10 * 60 * 1000), (2 * 60 * 60 * 1000)], // every 10 minutes starting at 2 hours
[(5 * 60 * 1000), (60 * 60 * 1000)], // every 5 minutes starting at 1 hour
[(2 * 60 * 1000), (5 * 60 * 1000)], // every 2 minutes starting at 5 minutes
[(30 * 1000), 0] // every 30 seconds starting at 0 minutes
];
var checkInterval = intervalRules[intervalRules.length-1][0];
var url = '//' + document.location.host + '/api/1/?task=autofingerlist';
function poll() {
$.ajax({ url: url, success: function(data) {
var updated = 0;
if (data && data.autofingerList) {
for(var i=0; i<data.autofingerList.length; i++) {
if(data.autofingerList[i].level <= Number(notificationLevel)) {
updated += data.autofingerList[i].usernames.length;
}
}
}
if (updated > 0) {
// update the page title (to update the tab, indicating new plans were found)
var level1Count = data.autofingerList[0].usernames.length;
var level2Count = data.autofingerList[1].usernames.length;
var level3Count = data.autofingerList[2].usernames.length;
if(document.title.match(/\(\d+\)/)){
$(document).attr('title', document.title.replace(/\(\d+\)/, '(' + updated + ')'));
} else {
if(notificationSide == 'right') {
$(document).attr('title', document.title + ' (' + updated + ')');
} else {
$(document).attr('title', '(' + updated + ') ' + document.title);
}
}
// Add indicators to autoread levels and refresh with new links
if (level1Count > 0) {
refreshAutofingerList (data.autofingerList[0].usernames, 1, level1Count);
}
if (level2Count > 0) {
refreshAutofingerList (data.autofingerList[1].usernames, 2, level2Count);
}
if (level3Count > 0) {
refreshAutofingerList (data.autofingerList[2].usernames, 3, level3Count);
}
}
}, dataType: "json", timeout: 10000});
for(var x=0; x<intervalRules.length; x++) {
var ruleInterval = intervalRules[x][0];
var ruleTime = intervalRules[x][1];
if(checkInterval < ruleInterval && totalTime >= ruleTime) {
checkInterval = ruleInterval;
clearInterval(interval);
interval = setInterval(poll, checkInterval);
break;
} else if(totalTime >= ruleTime) {
// interval is already high enough. no need to check the smaller timeouts.
break;
}
}
totalTime += checkInterval;
}
if(Number(notificationLevel) > 0) {
poll();
interval = setInterval(poll, 30000);
}
}
var plansPlusToInject = document.createElement("script");
plansPlusToInject.textContent = "(" + plansPlus.toString() + ")();";
document.body.appendChild(plansPlusToInject);