// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
//
// Links to read through
// http://ehsanakhgari.org/blog/2010-01-07/bugzilla-tweaks-enhanced
// http://hg.mozilla.org/users/ehsan.akhgari_gmail.com/extensions/file/tip/bugzillatweaks
// http://hg.mozilla.org/users/ehsan.akhgari_gmail.com/extensions/file/ecfa0f028b81/bugzillatweaks/lib/main.js
// http://hg.mozilla.org/users/avarma_mozilla.com/atul-packages/file/42ac1e99a107/packages\
// /facebook-acquaintances/lib/main.js#l11
// http://ehsanakhgari.org/blog/2010-05-31/my-experience-jetpack-sdk#comment-1253
//
"use strict";
var browser = require("tab-browser");
var self = require("self");
var pageMod = require("page-mod");
var libbz = require("libbugzilla");
var tabs = require("tabs");
var logger = require("logger");
var Message = require("util").Message;
function isOurPage(window, matchingURLs) {
var url = window.location.href;
// like ["regexp-url1", "regexp-url2"]
return matchingURLs.some(function (element,i,a) {
return new RegExp(element).test(url);
});
}
/**
*
*/
function skipThisPage(doc) {
var stemURL = "https://HOSTNAME/show_bug.cgi?id=";
var titleElems = doc.getElementsByTagName("title");
var titleStr = titleElems[0].textContent;
var REArr = new RegExp("[bB]ug\\s+([0-9]+)").exec(titleStr);
var hostname = doc.location.hostname;
if (REArr) {
var bugNo = REArr[1];
doc.location = stemURL.replace("HOSTNAME",hostname) + bugNo;
}
}
var messageHandler = exports.messageHandler = function messageHandler(worker, msg) {
console.log("messageHandler: msg = " + msg.toSource());
switch (msg.cmd) {
case "LogMessage":
console.log(msg.data);
break;
case "ExecCmd":
libbz.executeCommand(msg.data);
break;
case "AddLogRecord":
logger.addLogRecord(msg.data);
break;
case "GenerateTS":
logger.generateTimeSheet();
break;
case "ClearTS":
logger.clearTimeSheet();
break;
case "ImportTS":
logger.importTimeSheet();
break;
case "GetInstalledPackages":
// send message with packages back
libbz.getInstalledPackages(msg.data, function (pkgsMsg) {
worker.postMessage(pkgsMsg);
});
break;
case "GetClipboard":
libbz.getClipboard(function (clipboard) {
worker.postMessage(new Message(msg.data, clipboard));
});
break;
case "ChangeJSONURL":
libbz.changeJSONURL();
break;
case "OpenURLinPanel":
libbz.openURLInNewPanel(msg.data);
break;
case "OpenURLinTab":
libbz.openURLInNewTab(msg.data);
break;
case "OpenStringInPanel":
libbz.openStringInNewPanel(msg.data);
break;
case "MakeXMLRPCall":
// url, login, method, params, callback
libbz.makeXMLRPCCall(msg.data.url, msg.data.login, msg.data.method,
msg.data.params, function(ret) {
worker.postMessage(new Message(msg.data.callRPC, ret));
});
break;
case "GetURL":
libbz.getURL(msg.data.url, function(stuff) {
worker.postMessage(new Message(msg.data.backMessage, stuff));
});
break;
case "OpenBugUpstream":
libbz.createUpstreamBug(msg.data.url, msg.data.subject, msg.data.comment);
break;
case "testReady":
// we ignore it here, interesting only in unit test
break;
default:
console.error(msg.toSource());
}
};
var contentScriptLibraries = {
"bugzilla.redhat.com": [
self.data.url("util.js"),
self.data.url("color.js"),
self.data.url("rhbzpage.js"),
self.data.url("bzpage.js")
]
};
libbz.initialize(libbz.config, function () {
pageMod.PageMod({
include: [
"https://bugzilla.redhat.com/show_bug.cgi?id=*"
],
contentScriptWhen: 'ready',
contentScriptFile: contentScriptLibraries["bugzilla.redhat.com"],
onAttach: function onAttach(worker, msg) {
console.log("worker: " + worker);
worker.on('message', function (msg) {
messageHandler(worker, msg);
});
}
});
});
pageMod.PageMod({
include: [
"https://bugzilla.redhat.com/process_bug.cgi",
"https://bugzilla.redhat.com/attachment.cgi"
],
contentScriptWhen: 'ready',
contentScriptFile: self.data.url("skip-bug.js")
});
//tabs.open("https://bugzilla.redhat.com/show_bug.cgi?id=595017");