Add mark librarian contributions
[plutor-userscripts:greasemonkey-scripts.git] / metafilter / mark_librarian_contributions / mark_librarian_contributions.user.js
1 // ==UserScript==
2 // @name           Metafilter mark librarian contributions
3 // @namespace      http://plutor.org/
4 // @description    Mark Jessamyn's colleagues' contributions on Metafilter
5 // @include        http://metafilter.com/*
6 // @include        http://*.metafilter.com/*
7 // ==/UserScript==
8
9 /* -- Configuration variables, sort of -------------------------------------- */
10
11 var showcontacts = 1;            // Boolean
12
13 var updateevery = 86400 * 1000;         // One day
14 var debug = 0;
15
16 var contactimg = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8%2F9hAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A%2FwD%2FoL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9gFBw8vA%2BfHrcEAAAIMSURBVDjLbZM9blRBEIS%2F7vfWuzKYRbKEbBkRQIJERIKQCEg4Aycw4jgkJFwAcQhERkJETAQEEPiHP6N989dF8N4aGzPJtDSt6qrqGmM6kgwEGOPNWAuwGHtw3Gz9uO6ACC0wdgzmExwSyAyTMLMJWpLiGDh27wOgj4jZz5OTB8%2Bev3i6ubXc6WZm%2FeZ1bGMLAY5ockzCy69h%2F%2FHDV4v5xktgBdCb2cbxt%2B%2F3Dg6PHt29cevK8uoSza6g%2FjKmmGgG5o2vX1erT18O3t6%2BudetJfSSrEOL9vv7%2FP3bN93W9ibd9n267Xto0o4FjpOGTf%2Fw8dBv39yzUwAzQwoiKjmC9EOwSLAUHg3oRikenJQZqAf81MQewMwoOdME5oU%2BVyyDYUDQEJ2MUhrQEDoPIIlSKrVWhNGnjOeGEaP3CI8g1UAIO7PGfl2kUihDplWw1UCfGxZTu4HRyEUTK%2F4nITGkRCkNHzJdCkwjWWE4otaGzNbxOS%2Bh1sKQBoLExirjQ2BnkukY0TSlVBcl1NrIZSBKoaWMZ%2BhsTOXoRIWIafg%2FACklSinklGklM0sF6t%2F8A3hAH04tXFij3H1w9xyheS7VrGWsJTza5LkIBQFRaz2noQfy7u7uuyf7%2B68PDg93UJhd2oPL6TTKa5%2BQhju3rn2W1M79xlrrwt13MeaGGWe2Pc4%2FNTOQHQNH7mPO%2FwDlpTq2DvtS9QAAAABJRU5ErkJggg%3D%3D";
17
18 /* -- Global variables ------------------------------------------------------ */
19
20 var clist = new Object();
21 var now = new Date();
22
23 /* -- Simple helper functions ----------------------------------------------- */
24
25 function mlc_get_cached_data() {
26     try {
27         clist = eval(GM_getValue("MefiLibrarians", ""));
28     } catch(e) {
29         GM_log("Error parsing cache");
30         clist = new Object();
31     }
32 }
33
34 function mlc_last_cache_update() {
35     if (!clist) return 0;
36     if (!clist.stamp) return 0;
37
38     if (debug) GM_log("Last updated at " + clist.stamp, 0);
39     return clist.stamp;
40 }
41
42 /* -- XMLHTTP request ------------------------------------------------------- */
43
44 function mlc_update_contact_list() {
45     /* GET http://www.metafilter.com/usercontacts/USERID */
46     var url = 'http://www.metafilter.com/usercontactsdata.mefi?user_id=292'; // Jessamyn
47     if (debug) GM_log("Getting " + url, 0);
48
49     GM_xmlhttpRequest({
50         method: "GET",
51         url:    url,
52         onload: mlc_got_contact_list });
53 }
54
55 function mlc_got_contact_list(details) {
56     if (details.readyState == 4 && details.status == 200) {
57         var xml = new DOMParser().parseFromString(details.responseText, 'text/xml');
58         mlc_handle_contact_list(xml);
59     }
60 }
61
62 /* -- Creates the list from the contact list page --------------------------- */
63
64 function mlc_handle_contact_sublist(root, tagname) {
65     var contacts = root.getElementsByTagName(tagname);
66
67     if (contacts) {
68         contacts = contacts[0].childNodes;
69
70         for (var i=0; i<contacts.length; ++i) {
71             var contact = contacts[i];
72             if (contact.tagName && contact.tagName.toUpperCase() == 'USER') {
73                 var rel = '';
74                 var id = '';
75                 var name = ''
76                 var dist;
77                 try {
78                     id = contact.getElementsByTagName('id')[0].childNodes[0].nodeValue;
79                     name = contact.getElementsByTagName('username')[0].childNodes[0].nodeValue;
80                 } catch (e) {
81                     // Can't find all the required bits
82                     continue;
83                 }
84
85                 try {
86                     rel = contact.getElementsByTagName('relationship')[0].childNodes[0].nodeValue;
87                 } catch(e) {}
88
89                 try {
90                     dist = contact.getElementsByTagName('distance')[0].childNodes[0].nodeValue;
91                 } catch(e) {}
92
93
94                 // Toss out any non-collegues
95                 if (!rel.match(/colleague/)) {
96                     continue;
97                 }
98
99                 var newc;
100                 if (clist.uid[id]) {
101                     newc = clist.uid[id];
102                     if (rel) newc.rel = rel;
103                     if (dist) newc.dist = dist;
104                 } else {
105                     newc = { id: id, name: name, rel: rel, dist: dist };
106                 }
107                 newc[tagname] = 1;
108                 clist.uid[id] = newc;
109                 clist.name[name] = newc;
110             }
111         }
112     }
113 }
114
115 function mlc_handle_contact_list(xml) {
116     var root = xml.documentElement;
117
118     var numcontacts = 0;
119     clist = { uid: {}, name: {} };
120     
121     mlc_handle_contact_sublist(root, 'contacts');
122
123     /* Save */
124     clist.stamp = now.valueOf();
125     GM_setValue("MefiLibrarians", clist.toSource());
126
127     /* Now mark contributions */
128     mlc_mark_contribs();
129 }
130
131 /* -- Marks contribs from contacts  ----------------------------------------- */
132
133 var alreadymarked = 0;
134 function mlc_mark_contribs() {
135     if (alreadymarked) return;
136     var t0 = new Date();
137
138     var userhrefre = new RegExp("^(http://(www\.)?metafilter\.com)?/user(\.mefi)?/");
139     var usernamehrefre = new RegExp("(http://(www\.)?metafilter\.com)?/username.mefi/");
140
141     for (var i in document.links) {
142         var l = document.links[i];
143         var lh = new String(l.href);
144         var key = lh.substr(lh.lastIndexOf("/")+1);
145         var obj = null;
146
147         if (lh.match(userhrefre)) {
148             obj = clist.uid[key];
149         }
150         else if (lh.match(usernamehrefre)) {
151             obj = clist.name[key];
152         }
153
154         if (obj) {
155             var ni = document.createElement("img");
156             ni.src = contactimg;
157             ni.title = 'This user is a librarian';
158             ni.style.borderWidth = '0';
159             l.parentNode.insertBefore(ni, l);
160         }
161     }
162
163     alreadymarked = 1;
164         if (debug) GM_log("Took " + (new Date() - t0) + "ms to mark");
165 }
166
167 /* -- Main ------------------------------------------------------------------ */
168
169 function mlc_init() {
170     /* Get cached data */
171     mlc_get_cached_data();
172
173     var url = location.href;
174     url = url.replace(/https?:\/\/([^\/]*\.)?metafilter.com/, '');
175
176     if (now - mlc_last_cache_update() > updateevery) {
177         /* Download page and update cache if it's old */
178         mlc_update_contact_list();
179     } else {
180         /* Otherwise, just mark the contributions immediately */
181         mlc_mark_contribs();
182     }
183 }
184
185 /* DO IT */
186 mlc_init();
187
188
189