aboutsummaryrefslogtreecommitdiffstats
path: root/lib/main.js
blob: 65e296e292b9221331545f66a10ce95fb7d96d6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 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;
var contextMenu = require("context-menu");

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) {
  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 "SetClipboard":
    libbz.setClipboard(msg.data);
    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 = [
  self.data.url('lib/urltest.js'),
  self.data.url("lib/jumpNextBug.js"),
  self.data.url("lib/util.js"),
<<<<<<< HEAD
  self.data.url("lib/queries.js"),
=======
>>>>>>> 3c073b249228b929ba3ef7c620ddb62220503ec1
  self.data.url("lib/preprocessDuplicates.js"),
  self.data.url("lib/viewSource.js"),
  self.data.url("lib/color.js"),
  self.data.url("lib/addNewLinks.js"),
<<<<<<< HEAD
  self.data.url("lib/bugzillaDOMFunctions.js"),
  self.data.url("lib/otherButtons.js"),
  self.data.url("lib/makeBacktraceAttachment.js"),
  self.data.url("lib/fixingAttMIME.js"),
=======
>>>>>>> 3c073b249228b929ba3ef7c620ddb62220503ec1
  self.data.url("lib/logging-front.js"),
  self.data.url('lib/bug-page-mod.js'),
  self.data.url("lib/rhbzpage.js"),
  self.data.url("lib/bzpage.js")
];

libbz.initialize(libbz.config, function () {
  pageMod.PageMod({
    include: [
      "https://bugzilla.redhat.com/show_bug.cgi?id=*",
      "https://bugzilla.mozilla.org/show_bug.cgi?id=*",
      "https://bugzilla.gnome.org/show_bug.cgi?id=*"
    ],
    contentScriptWhen: 'ready',
    contentScriptFile: contentScriptLibraries,
    onAttach: function onAttach(worker, msg) {
      worker.on('message', function (msg) {
        messageHandler(worker, msg);
      });
    }
  });
});

pageMod.PageMod({
  include: [
    "https://bugzilla.redhat.com/process_bug.cgi",
    "https://bugzilla.redhat.com/post_bug.cgi",
    "https://bugzilla.redhat.com/attachment.cgi",
    "https://bugzilla.mozilla.org/process_bug.cgi",
    "https://bugzilla.mozilla.org/post_bug.cgi",
    "https://bugzilla.mozilla.org/attachment.cgi",
    "https://bugzilla.gnome.org/process_bug.cgi",
    "https://bugzilla.gnome.org/post_bug.cgi",
    "https://bugzilla.gnome.org/attachment.cgi"
  ],
  contentScriptWhen: 'ready',
  contentScriptFile: self.data.url("lib/skip-bug.js")
});


// Allow toggling of CC event displays using a context menu entry
contextMenu.Item({
  label: "Toggle CC History",
  contentScriptFile: [self.data.url('lib/urltest.js'),
                      self.data.url('lib/cc-context.js')]
});

contextMenu.Item({
  label: "Copy Check-in Comment",
  contentScriptFile: [self.data.url('lib/urltest.js'),
                      self.data.url('lib/checkin-context.js')],
  onMessage: function (comment) {
      require("clipboard").set(comment);
  }
});