-
Notifications
You must be signed in to change notification settings - Fork 1
/
newlove.user.js
173 lines (150 loc) · 6.12 KB
/
newlove.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
/*
Copyright 2007-2011 Ian Young
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Revision History
// 1.0 - Initial release
// 1.1 - Added ability to watch everyone's quicklove
// 1.2 - Erin [nichols] reworked this to work in Chrome!
// 1.3 - Use native JSON for storage, fix problems with FF4
// 1.4 - Updates for GreaseMonkey 3.0
// ==UserScript==
// @name NewLove
// @version 1.4.1
// @namespace http://www.grinnellplans.com
// @description Shows only new planlove in the quicklove page.
// @downloadUrl https://github.com/grinnellplans/Newlove/raw/master/newlove.user.js
// @include http://www.grinnellplans.com/search.php?mysearch=*&planlove=1*
// @match http://www.grinnellplans.com/search.php?mysearch=*&planlove=1*
// @include http://grinnellplans.com/search.php?mysearch=*&planlove=1*
// @match http://grinnellplans.com/search.php?mysearch=*&planlove=1*
// @include https://www.grinnellplans.com/search.php?mysearch=*&planlove=1*
// @match https://www.grinnellplans.com/search.php?mysearch=*&planlove=1*
// @include https://grinnellplans.com/search.php?mysearch=*&planlove=1*
// @match https://grinnellplans.com/search.php?mysearch=*&planlove=1*
// @grant GM_registerMenuCommand
// @grant GM_log
// ==/UserScript==
/* Credit Douglas Crockford <http://javascript.crockford.com/remedial.html> */
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, "");
};
// Gets the name of the author a given result is associated with
function getAuthor(node) {
var links = node.getElementsByTagName('a');
return links[0].textContent;
}
// Check given item against all members of the given array. Returns true if the item is found in the array
function arrayContains(arr, obj) {
if (!arr) return false;
for (var i=0; i<arr.length; i++) {
if (arr[i].trim() == obj.trim()) {
return true;
}
}
return false;
}
// Reset the username and history (simulate a fresh install)
function resetValues(e) {
if (window.confirm("Reset username and saved planlove?")) {
localStorage.removeItem("username");
localStorage.removeItem("planloveHash" + guessUsername);
}
}
// Do not count new planlove as read
function saveOldlove() {
localStorage.setItem("planloveHash" + guessUsername, JSON.stringify(oldlove));
}
// Compatibility proxy to only run Greasemonkey commands when in Greasemonkey.
var rootScope = this;
var Greasy = {
registerMenuCommand: function(a, b, c) {
if (typeof GM_registerMenuCommand != 'undefined') {
GM_registerMenuCommand(a, b, c);
}
}
, log: function(message) {
if (typeof GM_log != 'undefined') {
GM_log(message);
}
}
};
(function() {
// Figure out if this is actually the quicklove page, as opposed to
// a regular search. Hackity hack!
username = localStorage.getItem("username");
// We need to determine the username. Let's make a guess based on
// the current url of the page.
var urly = window.location.href;
var startIndex = urly.indexOf("mysearch=") + 9;
var endIndex = urly.indexOf("&", startIndex);
var guessUsername = urly.substring(startIndex, endIndex);
if (!username) {
// Ask for confirmation of the username
username = window.prompt("What's your username?\n\nIf you want to stalk other people's newlove as well as your own, enter 'everyone' here.", guessUsername).toLowerCase();
if (!username) return false; // give up
localStorage.setItem("username", username);
}
// Now, if the page we're currently on isn't searching for that
// username, fuggedaboudit.
if (username != guessUsername && username != "everyone") {
Greasy.log("False alarm, this isn't a quicklove page. Exiting.");
return false;
}
// Add items to the menu
Greasy.registerMenuCommand("Reset username", resetValues, "", "", "R");
Greasy.registerMenuCommand("Save as unread", saveOldlove, "", "", "u");
// Find all 'sub-lists' in the page
var loves = document.evaluate(
'//ul[@id="search_results"]/li//ul/li',
document,
null,
XPathResult.UNORDERED_NODE_ITERATOR_TYPE,
null);
// Get the stored planlove from last time
oldlove_str = localStorage.getItem("planloveHash" + guessUsername);
// Convert from the stored string to a hashtable of arrays
try {
var oldlove = JSON.parse( oldlove_str );
// Test it to see if it's null
oldlove["foo"];
} catch (e) {
var oldlove = {};
}
// A running list of all quicklove received
var newlove = {};
// Read quicklove, to be hidden
var toRemove = []
// Iterate through the list of search results
var thisLoveNode;
while ( thisLoveNode = loves.iterateNext() ) {
var author = getAuthor(thisLoveNode.parentNode.parentNode);
thisLoveText = thisLoveNode.textContent;
// Check each lovin' against list of author's previous lovin'
if (arrayContains(oldlove[author], thisLoveText)) {
// Mark for removal
toRemove.push( thisLoveNode );
}
// Fetch the array of planlove for the current author
var temp_arr = newlove[author];
// Create it if it doesn't exist
if (!newlove[author]) { newlove[author] = []; }
// Add it to the new list of planlove
newlove[author].push( thisLoveText );
}
// Now remove all old love
toRemove.forEach( function( n ) {
n.parentNode.removeChild( n );
});
// Store the new list of planlove, for next time
localStorage.setItem("planloveHash" + guessUsername, JSON.stringify(newlove));
})()