~gm-dev-launchpad/launchpad-gm-scripts/master

90 by Brian Murray
adding patches, reporter comments, and prefill question comment into a single gm script
1
// ==UserScript==
2
// @name           LP_Improvements
3
// @namespace      http://murraytwins.com/greasemonkey/
4
// @description    (Launchpad) Collection of scripts that modify Launchpad
5
// @include        https://launchpad.net/*
6
// @include        https://*.launchpad.net/*
7
// @include        https://*.edge.launchpad.net/*
8
// @include        https://launchpad.net/*
9
// @date           2008-08-24
10
// @creator        Brian Murray <brian@ubuntu.com>
11
// ==/UserScript==
12
13
// ------  User settable data  -------------
14
15
// Color for the comment heading in reporter_comments()
16
17
// used in reporter_comments()
18
var color = 'lightgrey';
19
20
// used in identify patches()
21
var star = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAABmJLR0QA%2FwD%2FAP%2BgvaeTAAAAB3RJTUUH1goWBRUW8tvU3gAAAVxJREFUKJGtkb1OAlEQhc%2Fc3b0QEAFJjGBhYmUiNMbYmdhZYelPzwPoK1j5DGa1s7EyMcbYaovBAgoKg0IhQSHosstyl8u1EaLhJ8Z4ypnzZU7mAP%2BtuonbhomLcXt9AruuJizZyGvHWCbObOLMbphI%2FhqEznZ5Yk7niVldMbYzykIAUDcxlGpqYw%2BQTbTuroegWAakf51dURrd8IVQxL%2BUMsi3BmjzgKwgnOZA9wluseSJZ6tJUm0OokYzyAlXJUXZytvZgqO8GiArQK8FJdqwsyVHlK28cFUymkFuELUvdQ7e%2BEAnsJqCEV8ElAfv5RHOfREz0%2FDRNsTgDd%2FBhoCfiDp6xPZ5lQcFAHqMETRy3yzlA8aA1EGahXnXLVR7otqWABSPBwwtZEh6F1sAzvreH3Uo0JFsdoLeq3spXRWXrkqImnMlmyIIRoejakG%2FlvoJ9ofmpzgYVdmf9Al84I7Jn6sOdQAAAABJRU5ErkJggg%3D%3D"; 
22
23
// used in prefill_question_comment()
24
var question_comment = "Thank you for taking the time to report this issue and helping to make Ubuntu better. Examining the information you have given us, this does not appear to be a bug report so we are closing it and converting it to a question in the support tracker. We appreciate the difficulties you are facing, but it would make more sense to raise problems you are having in the support tracker at https://answers.launchpad.net/ubuntu if you are uncertain if they are bugs. For help on reporting bugs, see https://help.ubuntu.com/community/ReportingBugs#When%20not%20to%20file%20a%20bug."
25
91 by Brian Murray
adding karma_suffix to unified script
26
// used in append_karma()
27
var teams = {
28
    'motu-swat':'',
29
//    'motu-release':'',
30
//    'motu-sru':'',
31
    'bugsquad':'',
129.1.1 by Nigel Babu
Add ubuntu-reviewers to team logos
32
    'ubuntu-reviewers':'',
91 by Brian Murray
adding karma_suffix to unified script
33
    'ubuntu-core-dev':'',
34
    'ubuntu-dev':'',
35
    'universe-contributors':'',
36
    'ubuntu-bugcontrol':'',
37
    'ubuntumembers':''
38
};
39
40
// used in append_karma()
41
var people_cache = new Array();
42
90 by Brian Murray
adding patches, reporter comments, and prefill question comment into a single gm script
43
// ------- End of User settable data -------/
44
45
(function() {
46
47
function xpath(query, context) {
48
//    GM_log('xpath running');
49
    context = context ? context : document;
50
    return document.evaluate(query, context, null,
51
                            XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
52
}
53
54
function requestHandler(req, fn, args, arg2)
55
{
56
    if (req.readyState == 4 && req.status == 200) {
57
        fn(req, args, arg2);
58
        //fn(req.responseXML, args);
59
        //fn(req.responseXML.documentElement, args);
60
    }
61
}
62
63
function loadData(URL, response_handler, response_arg, response_arg2)
64
{
65
    //alert("Fetching " + response_arg + " ("+URL+")");
66
67
    // Create the XML request
68
    var httpRequest;
69
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
70
            httpRequest = new XMLHttpRequest();
71
            if (httpRequest.overrideMimeType) {
72
                httpRequest.overrideMimeType('text/xml');
73
                // See note below about this line
74
            }
75
    } else if (window.ActiveXObject) { // IE
76
            try {
77
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
78
            } catch (e) {
79
                try {
80
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
81
                } catch (e) {}
82
            }
83
    }
84
    if (!httpRequest) {
85
        return null;
86
    }
87
88
    // Anonymous function to handle changed request states
89
    httpRequest.onreadystatechange = function() { requestHandler(httpRequest, response_handler, response_arg, response_arg2); };
90
91
    // Make the request
92
    httpRequest.open ('GET', URL);
93
    //httpRequest.overrideMimeType('text/xhtml');
94
    httpRequest.overrideMimeType('text/xml');
95
    httpRequest.send (null);
96
}
97
98
function patch_handler(xmldoc, librarian_link, comment) {
99
    var text = xmldoc.responseText.replace(/\n/g," ");
100
101
//    if (xpath("//input[contains(@name,'field.patch') and contains(@value,'on')]", xmldoc.responseText)) {
102
    var re = new RegExp("checked=\"checked\" id=\"field.patch\"");
103
    if ((match = re.exec(text)) != null) {
104
        if (debug) {
105
            GM_log("Patch found " + librarian_link);
106
        }
107
        librarian_link.parentNode.setAttribute("style", "list-style-image: url(" + star + ")");
108
        for (var j = 0; j < comment.snapshotLength; j++) {
109
            if (debug) 
110
                GM_log("Comment url " + comment.snapshotItem(j));
111
            if ( String(librarian_link) == String(comment.snapshotItem(j))) {
112
                comment.snapshotItem(j).parentNode.setAttribute("style", "list-style-image: url(" + star + ")");
113
            }
114
        } 
115
        
116
    }
117
}
118
119
function reporter_comments() {
113 by Bryce Harrington
Fix broken reporter due to launchpad UI change
120
    var reporter = xpath("//*[@class='registering']/a[@class='sprite person']").snapshotItem(0);
90 by Brian Murray
adding patches, reporter comments, and prefill question comment into a single gm script
121
122
    if (debug) {
123
        GM_log( "reporter href " + reporter );
124
    }
125
126
    var commenters = xpath("//div[@class='boardCommentDetails']/a");
127
     
128
    for ( var i = 0; i < commenters.snapshotLength; i++ ) {
129
        var commenter = commenters.snapshotItem(i);
130
        if (debug) {
131
            GM_log( "commenter href " + commenter );
132
        } 
133
134
        if ( String(commenter) == String(reporter) ) {
135
            if (debug) {
136
                GM_log( "Found a match" );
137
            }
138
139
            var css_style = "background:" + color + ";";
140
            commenter.parentNode.setAttribute('style', css_style);
141
 
142
        }
143
    }
144
}
145
146
function identify_patches() {
147
    
148
    var librarian = xpath("//div[contains(@id,'portlet-attachments')]//li[contains(@class,'download')]/a");
149
    var comment = xpath("//div[contains(@class,'boardCommentBody')]//ul//li[contains(@class,'download')]/a");
150
    if (debug) {
151
        GM_log("comment is "+comment);
152
        GM_log("librarian is "+librarian);
153
    }
154
    for (var i = 0; i < librarian.snapshotLength; i++) {
155
        var librarian_link = librarian.snapshotItem(i);
156
        var edit = xpath("//div[contains(@id,'portlet-attachments')]//li[contains(@class,'download')]/small/a");
157
        if (debug)
158
            GM_log("edit url "+edit.snapshotItem(i));
159
        var edit_link = edit.snapshotItem(i);
160
        loadData(edit_link, patch_handler, librarian_link, comment);
161
    }
162
}
163
164
function prefill_question_comment() {
165
166
    if (xpath('//div[@class="actions"]/input[@value="Convert this Bug into a Question"]').snapshotItem(0)) {
167
        xpath('//textarea[@id="field.comment"]').snapshotItem(0).value = comment;
168
    }
169
}
91 by Brian Murray
adding karma_suffix to unified script
170
//from lp_karma_suffix.user.js
171
function augment_person(person) {
172
    for (var idx in people_cache[person]['nodes']) {
173
         var node = people_cache[person]['nodes'][idx];
174
175
        for (var title in people_cache[person]['team']) {
176
            var logoNode = document.createElement('img');
177
            logoNode.title = title;
178
            logoNode.src = people_cache[person]['team'][title];
179
            node.parentNode.insertBefore(logoNode, node.nextSibling);
180
        }
181
       
182
        // add in karma value and last paren
183
        var karmaNode = document.createTextNode(': '+people_cache[person]['karma']+') ');
184
        node.parentNode.insertBefore(karmaNode, node.nextSibling);
185
        // add in contact user link
186
        var contactLink = document.createElement("a");
187
        contactLink.href = "https://launchpad.net/~" +person+ "/+contactuser";
188
        contactLink.title = "Contact this user";
189
        contactLink.innerHTML = person;
190
        node.parentNode.insertBefore(contactLink, node.nextSibling);
191
        // add in a 1st paren
192
        var firstNode = document.createTextNode(' (');
193
        node.parentNode.insertBefore(firstNode, node.nextSibling);
194
    }
195
}
196
// from lp_karma_suffix.user.js
197
// Handles extracting the karma details
198
function karma_handler(xmldoc, person) {
199
    // The output of LP can't be used as an XML document, so we're forced
200
    // to build a screen-scraper!!  (LP #92853)
201
    var text = xmldoc.responseText.replace(/\n/g," ");
202
203
    var karma = '0';
204
205
    // XPath method... 
206
    //var results = xpath("//span[contains(@id,'karma-total')]", xmldoc.responseXML);
207
    //if (results.snapshotLength>0) {
208
    //    karma = results.snapshotItem(0).firstChild.nodeValue;
209
    //    //alert("Karma for "+person+": "+karma);
210
    //}
211
212
    // screen-scraping method...
213
    var span = '/+karma">';
214
    var karma_at = text.indexOf(span);
215
    if (karma_at>0) {
216
        var karma_all = text.substr(karma_at+span.length);
217
        karma = karma_all.substr(0,karma_all.indexOf("</a>"));
218
        if (debug)
219
            GM_log("Karma for "+person+": "+karma);
220
    }
221
222
    // store for later
223
    people_cache[person]['karma'] = karma;
224
225
    // end of chain, display
226
    augment_person(person);
227
}
228
// from lp_karma_suffix.user.js
229
// Handles extracting the team details
230
function team_handler(xmldoc, person) {
231
    // The output of LP can't be used as an XML document, so we're forced
232
    // to build a screen-scraper!!  (LP #92853)
233
    var text = xmldoc.responseText.replace(/\n/g," ");
234
235
    // skip non-humans (this will abort the fetching chain)
236
    if (text.indexOf('Show all members')>=0) {
237
        return;
238
    }
239
240
    var re = new RegExp("<img[^>]* src=\"(https://launchpadlibrarian.net/[^\"]+)\"[^>]+>[^a]*<a[^>]* href=\"[^~\"]*/~([^\"]+)\"","ig");
241
    while ((match = re.exec(text)) != null) {
242
        title = match[2];
243
        src = match[1];
244
        if (title in teams) {
245
            people_cache[person]['team'][title] = src;
246
            if (debug)
247
                GM_log(title+' has URL '+src+' for '+person);
248
        }
249
    }
250
251
    // chain to next item to load...
252
    loadData(people_cache[person]['karma_link'], karma_handler, person);
253
}
254
// from lp_karma_suffix.user.js
255
function add_people(people) {
256
  for (var i = 0; i < people.snapshotLength; i++) {
257
    var node = people.snapshotItem(i);
258
    var link = "" + node;
259
260
    var person  = link.substr(link.lastIndexOf("~")+1);
261
262
    // Detect and drop sub directory matches
263
    var slash = person.indexOf("/");
264
    if (slash != -1) {
265
        // fix up person name
266
        person = person.substr(0,slash);
267
        // fix up link
268
        link = link.substr(0,link.indexOf("/",link.indexOf("~")));
269
    }
270
271
    if (!people_cache[person]) {
272
        people_cache[person] = new Array();
273
        people_cache[person]['team_link'] = link + "/+participation";
274
        people_cache[person]['karma_link'] = link + "/@@+portlet-details";
275
        people_cache[person]['email_link'] = link + "/@@+portlet-email";
276
        people_cache[person]['nodes'] = new Array();
277
        people_cache[person]['team'] = new Array();
278
        if (debug)
279
            GM_log(person);
280
    }
281
    people_cache[person]['nodes'].push(node);
282
  }
283
}
284
// from lp_karma_suffix.user.js
285
function append_karma() {
286
    var prefix = new Array("https://launchpad.net",
287
                           "https://bugs.launchpad.net",
288
                           "https://edge.launchpad.net",
289
                           "https://bugs.edge.launchpad.net",
290
                           "");
291
    var url_clean_matches = new Array();
292
    var url_messy_matches = new Array();
293
    for (var idx in prefix) {
294
        url_clean_matches.push("(starts-with(@href, '"+prefix[idx]+"/~') and not(contains(substring-after(@href, '"+prefix[idx]+"/~'),'/')))");
295
        url_messy_matches.push("(starts-with(@href, '"+prefix[idx]+"/~') and contains(substring-after(@href, '"+prefix[idx]+"/~'),'/+'))");
296
    }
297
    var a_clean_match = "a["+url_clean_matches.join(" or ")+"]";
298
    var a_messy_match = "a["+url_messy_matches.join(" or ")+"]";
299
300
    // All the people links in the main content section (_not_ subscribers!)
301
    add_people(xpath("//div[contains(@id,'maincontent')]//"+a_clean_match));
302
303
    // Bug reporter
304
    add_people(xpath("//*[@class='object timestamp']/"+a_clean_match));
305
306
    // Assignees
307
    add_people(xpath("//table[contains(@id,'affected-software')]//td/"+a_clean_match));
308
309
    // Everyone!  (this is totally insane for bugs with large dups)
310
    //add_people(xpath("//"+a_match));
311
312
    // Go fetch all found people's info and attach it to their nodes
313
    for (var person in people_cache) {
314
        loadData(people_cache[person]['team_link'], team_handler, person);
315
    }
316
}
90 by Brian Murray
adding patches, reporter comments, and prefill question comment into a single gm script
317
318
window.addEventListener("load", function(e) {
319
320
    var debug = 0
321
    
322
    reporter_comments();
323
    identify_patches();
324
    prefill_question_comment();
91 by Brian Murray
adding karma_suffix to unified script
325
    append_karma();
90 by Brian Murray
adding patches, reporter comments, and prefill question comment into a single gm script
326
  
327
    }, false);
328
})();