nmbug.js: Cleanup dialog-polyfill injection to avoid duplicates
authorW. Trevor King <wking@tremily.us>
Sun, 21 Sep 2014 22:14:03 +0000 (15:14 -0700)
committerW. Trevor King <wking@tremily.us>
Sun, 21 Sep 2014 22:14:03 +0000 (15:14 -0700)
The bookmarklet used to load these each time it was launched, which
caused duplicate entries if you opened and closed the bookmarklet
multiple times on the same page.

nmbug.js

index 4281cdb8f4e4a6574b2d131181d48f0b4c474048..6a466f6c842ab38fa7c8c66870a2d242ef01d4f1 100644 (file)
--- a/nmbug.js
+++ b/nmbug.js
@@ -53,27 +53,64 @@ nmbug = {
                        this._x_edit_tags(available_tags, message_id, tags);
                } else {
                        var _this = this;
-                       var dialog_polyfill_loaded = 0;
+                       var needed = [];
+
+                       function basename (element) {
+                               var url;
+                               if (!element.tagName) {
+                                       return;
+                               } else if (element.tagName.toLowerCase() == 'script') {
+                                       url = element.src;
+                               } else if (element.tagName.toLowerCase() == 'link') {
+                                       url = element.href;
+                               } else {
+                                       return;
+                               }
+                               return url.replace(/.*\//, '');
+                       }
 
                        function onload () {
-                               dialog_polyfill_loaded++;
-                               if (dialog_polyfill_loaded == 2) {
+                               var name = basename(this);
+                               console.log('nmbug: loaded ' + name, this);
+                               var index = needed.indexOf(name);
+                               if (index !== -1) {
+                                       needed.splice(index, 1);
+                               }
+                               if (needed.length == 0) {
                                        _this._x_edit_tags(available_tags, message_id, tags);
                                }
                        }
 
-                       var script = document.createElement('script');
-                       script.type = 'text/javascript';
-                       script.src = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.js';
-                       script.onload = onload;
-                       document.head.appendChild(script);
+                       function has_header (name) {
+                               var nodes = document.head.childNodes;
+                               for (var i = 0; i < nodes.length; i++) {
+                                       if (basename(nodes[i]) == name) {
+                                               return true;
+                                       }
+                               }
+                               return false;
+                       }
+
+                       if (!has_header('dialog-polyfill.js')) {
+                               needed.push('dialog-polyfill.js');
+                               var script = document.createElement('script');
+                               script.type = 'text/javascript';
+                               script.src = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.js';
+                               script.onload = onload;
+                               console.log('nmbug: loading dialog-polyfill.js');
+                               document.head.appendChild(script);
+                       }
 
-                       var link = document.createElement('link');
-                       link.rel = 'stylesheet';
-                       link.type = 'text/css';
-                       link.href = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.css';
-                       link.onload = onload;
-                       document.head.appendChild(link);
+                       if (!has_header('dialog-polyfill.css')) {
+                               needed.push('dialog-polyfill.css');
+                               var link = document.createElement('link');
+                               link.rel = 'stylesheet';
+                               link.type = 'text/css';
+                               link.href = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.css';
+                               link.onload = onload;
+                               console.log('nmbug: loading dialog-polyfill.css');
+                               document.head.appendChild(link);
+                       }
                }
        },
        _x_edit_tags: function (available_tags, message_id, tags) {