From fe31ab80e42372f2b4ad9b7b21f7027cea37c2e6 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 20 Sep 2014 11:46:37 -0700 Subject: [PATCH] nmbug.js: Sketch out the bookmarklet framework We'll have a generic nmbug.show() to handle the UI for tagging a given message (using its Message-ID). To get that Message-ID, we'll have a list of potential handlers. When the bookmarklet fires (run()), we'll iterate through the handlers unril handler.regexp matches document.URL. For the first match, we'll run handler.handle, which will do whatever it needs to figure out the Message-ID, and then launch nmbug.show (passed in via 'callback') with the extracted id. --- nmbug.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 nmbug.js diff --git a/nmbug.js b/nmbug.js new file mode 100644 index 0000000..214c9f2 --- /dev/null +++ b/nmbug.js @@ -0,0 +1,33 @@ +nmbug = { + show: function (message_id) { + alert(message_id); + }, +}; + +var _gmane_handler = { + regexp: /gmane[.]org/, + handle: function (callback) { + throw 'Extract the Message-ID from ' + document.URL; + }, +}; + +var handlers = [ + _gmane_handler, +]; + +function _check_handler(handler) { + var match = handler.regexp.test(document.URL); + console.log('nmbug: testing', handler, match); + if (match) { + console.log('nmbug: matched', handler); + handler.handle(nmbug.show); + } + return match; /* break after the first match */ +} + +function run() { + var matched = handlers.some(_check_handler); + if (!matched) { + throw 'No handler for ' + document.URL; + } +} -- 2.26.2