+/*
+ Copyright (C) 2014 W. Trevor King <wking@tremily.us>
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ POSSIBILITY OF SUCH DAMAGE.
+ */
+
var nmbug_server = 'http://localhost:5000';
nmbug = {
- show: function (message_id) {
+ show: function (message_id, frame) {
var _this = this;
+ if (frame === undefined) {
+ frame = window;
+ }
this._get_available_tags(function (available_tags) {
_this._get_tags(
message_id,
- _this._edit_tags.bind(_this, available_tags, message_id));
+ _this._edit_tags.bind(_this, frame, available_tags, message_id));
});
},
_get_available_tags: function (callback) {
request.open('get', url, true);
request.send();
},
- _edit_tags: function (available_tags, message_id, tags) {
- if (document.createElement('dialog').show) {
- this._x_edit_tags(available_tags, message_id, tags);
- } else {
- var _this = this;
- 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(/.*\//, '');
+ _edit_tags: function (frame, available_tags, message_id, tags) {
+ var have_dialog_polyfill;
+ try {
+ have_dialog_polyfill = frame.dialogPolyfill !== undefined;
+ } catch (error) {
+ if (error.name == 'ReferenceError') {
+ have_dialog_polyfill = false;
}
+ }
+ if (frame.document.createElement('dialog').show || have_dialog_polyfill) {
+ this._x_edit_tags(frame, available_tags, message_id, tags);
+ } else {
+ var script = frame.document.createElement('script');
+ script.type = 'text/javascript';
+ script.src = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.js';
+ script.async = false;
+ console.log('nmbug: loading dialog-polyfill.js');
+ frame.document.head.appendChild(script);
- function onload () {
- 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 link = frame.document.createElement('link');
+ link.rel = 'stylesheet';
+ link.type = 'text/css';
+ link.href = nmbug_server + '/static/dialog-polyfill/dialog-polyfill.css';
+ link.async = false;
+ console.log('nmbug: loading dialog-polyfill.css');
+ frame.document.head.appendChild(link);
- function has_header (name) {
- var nodes = document.head.childNodes;
- for (var i = 0; i < nodes.length; i++) {
- if (basename(nodes[i]) == name) {
- return true;
+ var _this = this;
+ function edit_tags_after_dialog_polyfill () {
+ try {
+ have_dialog_polyfill = frame.dialogPolyfill !== undefined;
+ console.log('have dialogPolyfill');
+ window.setTimeout(
+ _this._x_edit_tags.bind(_this), 200,
+ frame, available_tags, message_id, tags);
+ } catch (error) {
+ if (error.name == 'ReferenceError') {
+ console.log('waiting for dialogPolyfill');
+ window.setTimeout(edit_tags_after_dialog_polyfill, 200);
}
}
- 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);
- }
-
- 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);
}
+ edit_tags_after_dialog_polyfill();
}
},
- _x_edit_tags: function (available_tags, message_id, tags) {
- var dialog = document.createElement('dialog');
- if (!document.createElement('dialog').show) {
- dialogPolyfill.registerDialog(dialog);
+ _x_edit_tags: function (frame, available_tags, message_id, tags) {
+ var dialog = frame.document.createElement('dialog');
+ if (!frame.document.createElement('dialog').show) {
+ frame.dialogPolyfill.registerDialog(dialog);
}
dialog.style.border = '1px solid rgba(0, 0, 0, 0.3)';
dialog.style.borderRadius = '6px';
dialog.style.boxShadow = '0 3px 7px rgba(0, 0, 0, 0.3)';
+ dialog.style.marginLeft = '10em';
+ dialog.style.marginRight = '10em';
- var content = document.createElement('p');
+ var content = frame.document.createElement('p');
content.innerHTML = 'Edit tags for ' + message_id;
dialog.appendChild(content);
- var ul = document.createElement('ul');
- dialog.appendChild(ul);
+ var tag_list = frame.document.createElement('p');
+ dialog.appendChild(tag_list);
for (var i = 0; i < available_tags.length; i++) {
- var li = document.createElement('li');
- li.innerHTML = available_tags[i];
- li.style.cursor = 'pointer';
+ var tag = frame.document.createElement('a');
+ tag.innerHTML = available_tags[i];
+ tag.style.cursor = 'pointer';
if (tags.indexOf(available_tags[i]) >= 0) {
- li.style.backgroundColor = 'lime';
+ tag.style.backgroundColor = 'lime';
}
- li.onclick = this._toggle_tag.bind(
- this, message_id, available_tags[i], li);
- ul.appendChild(li);
+ tag.onclick = this._toggle_tag.bind(
+ this, message_id, available_tags[i], tag);
+ tag_list.appendChild(tag);
+ tag_list.appendChild(frame.document.createTextNode(' '));
}
- var close = document.createElement('button');
+ var close = frame.document.createElement('button');
close.innerHTML = 'Close';
close.onclick = function () {
dialog.close();
};
dialog.appendChild(close);
- document.body.appendChild(dialog);
+ frame.document.body.insertBefore(dialog, frame.document.body.firstChild);
dialog.show();
},
- _toggle_tag: function (message_id, tag, li) {
+ _toggle_tag: function (message_id, tag, element) {
var prefix;
- if (li.style.backgroundColor == 'lime') {
+ if (element.style.backgroundColor == 'lime') {
prefix = '-'; /* unset */
- li.style.backgroundColor = null;
+ element.style.backgroundColor = null;
} else {
prefix = '+'; /* set */
- li.style.backgroundColor = 'lime';
+ element.style.backgroundColor = 'lime';
}
var url = [
nmbug_server,
var _gmane_handler = {
regexp: /gmane[.]org/,
handle: function (callback) {
- var article = this._get_article();
- this._get_message_id(article, callback);
+ var frame = this._get_frame();
+ var article = this._article_from_url(frame.document.URL);
+ this._get_message_id(article, function (message_id) {
+ callback(message_id, frame);
+ });
},
_article_from_url: function (url) {
var regexp = new RegExp('http://article.gmane.org/([^/]+)/([0-9]+)');
return {'group': match[1], 'id': parseInt(match[2])};
}
},
- _get_article: function () {
- var article = this._article_from_url(document.URL);
+ _get_frame: function () {
+ var frame = window;
+ var article = this._article_from_url(frame.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);
+ frame = window.frames[i];
+ article = this._article_from_url(frame.document.URL);
}
if (!article) {
throw "Cannot extract an article from Gmane's " + document.URL;
}
- return article;
+ return frame;
},
_get_message_id: function (article, callback) {
var url = [