nmbug.js: Extract the Gmane article id and convert to a Message-ID
authorW. Trevor King <wking@tremily.us>
Sat, 20 Sep 2014 20:00:23 +0000 (13:00 -0700)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Sep 2014 20:00:23 +0000 (13:00 -0700)
Using some JavaScript gymnastics to look through the available frames
for an article.gmane.org/ URL.  For example, we might be on a page
like:

  http://thread.gmane.org/gmane.mail.notmuch.general/19055/focus=19056

which is composed of the following frames [1]:

  http://news.gmane.org/group/gmane.mail.notmuch.general/thread=19055/force_load=t/focus=19056
  http://article.gmane.org/gmane.mail.notmuch.general/19056
  http://news.gmane.org/navbar.php?group=gmane.mail.notmuch.general&article=19056&next=19057&prev=19054&newsrc=,19056

Or we might be on the article page directly.  There are also permalink
pages (with 'permalink' instead of 'article' available from the
blog-link view), but I'm not worrying about them yet.

[1]: for (var i = 0; i < window.frames.length; i++) {
       console.log(window.frames[i].document.URL);
     }

nmbug.js

index 214c9f2d38a0f74e4a1815fbcec696ac9da15136..5a8a14142c51b43973fdee754907f08eef1cb4e7 100644 (file)
--- a/nmbug.js
+++ b/nmbug.js
@@ -1,3 +1,5 @@
+var nmbug_server = 'http://localhost:5000';
+
 nmbug = {
        show: function (message_id) {
                alert(message_id);
@@ -7,7 +9,43 @@ nmbug = {
 var _gmane_handler = {
        regexp: /gmane[.]org/,
        handle: function (callback) {
-               throw 'Extract the Message-ID from ' + document.URL;
+               var article = this._get_article();
+               this._get_message_id(article, callback);
+       },
+       _article_from_url: function (url) {
+               var regexp = new RegExp('http://article.gmane.org/([^/]+)/([0-9]+)');
+               var match = regexp.exec(url);
+               console.log('nmbug: get article from ' + url, match);
+               if (match) {
+                       return {'group': match[1], 'id': parseInt(match[2])};
+               }
+       },
+       _get_article: function () {
+               var article = this._article_from_url(document.URL);
+               var i = 0;
+               for (var i = 0; !article && i < window.frames.length; i++) {
+                       article = this._article_from_url(window.frames[i].document.URL);
+               }
+               if (!article) {
+                       throw "Cannot extract an article from Gmane's " + document.URL;
+               }
+               return article;
+       },
+       _get_message_id: function (article, callback) {
+               var url = [
+                       nmbug_server,
+                       'gmane',
+                       article.group,
+                       article.id,
+               ].join('/');
+               console.log('nmbug: get Message-ID from ' + url);
+               var request = new XMLHttpRequest();
+               request.onload = function () {
+                       var message_id = this.responseText;
+                       callback(message_id);
+               };
+               request.open('get', url, true);
+               request.send();
        },
 };