From b93883aab324ed864463d8fe72f71ac27fcc9240 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 21 Sep 2014 15:14:03 -0700 Subject: [PATCH] nmbug.js: Cleanup dialog-polyfill injection to avoid duplicates 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 | 65 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/nmbug.js b/nmbug.js index 4281cdb..6a466f6 100644 --- 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) { -- 2.26.2