1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
10 ////////////////////////////////////////////////////////////////////////////////
11 uses("resource://gre/modules/devtools/gcli.jsm");
18 function buildFileList (list
) {
20 for (let idx
= 0; idx
< list
.length
; ++idx
) {
23 it
.name
= (xn
.islib
? "/lib/"+xn
.name
: xn
.name
);
29 function addjsext (fn
) {
30 if (!/\.js$/.test(fn
)) fn
+= ".js";
35 function openEditor (fname
, chromeWin
, newfile
) {
36 let fl
= Cc
["@mozilla.org/file/local;1"].createInstance(Ci
.nsIFile
);
37 fl
.followLinks
= true;
38 fl
.initWithPath(fname
);
41 if (fl
.isDirectory()) return;
42 lmtime
= fl
.lastModifiedTime
;
48 text
= scacheAPI
.fileReadText(fl
);
52 logError("can't read file: "+fname
);
56 "// ==UserScript==\n"+
61 "// @run-at document-end\n"+
65 "// ==/UserScript==\n\n";
67 let spw
= chromeWin
.Scratchpad
.ScratchpadManager
.openScratchpad({
72 // TODO: how can i observe file changes without timer?
73 let wasChange
= false;
74 let checkFC = function () {
75 let fl
= Cc
["@mozilla.org/file/local;1"].createInstance(Ci
.nsIFile
);
76 fl
.followLinks
= true;
77 fl
.initWithPath(fname
);
78 if (fl
.exists() && !fl
.isDirectory() && lmtime
!= fl
.lastModifiedTime
) {
79 lmtime
= fl
.lastModifiedTime
;
84 let tm
= intervalTimer(function () {
85 if (checkFC()) { scacheAPI
.reset(); wasChange
= true; }
87 // kill timer on closing
88 spw
.addEventListener("close", function sploaded() {
89 spw
.removeEventListener("close", sploaded
, false);
92 if (wasChange
|| checkFC()) {
95 latestUC
= scacheAPI
.getUC();
101 let gcliCommandSpecs
= [
104 description
: "GuerillaJS control",
108 name
: "guerilla about",
109 description
: "show various info",
112 description
: "info to show",
113 type
: { name
: "selection", data
: ["readme", "credits", "thanks", "licenses"] },
114 defaultValue
: "readme",
117 exec: function (args
, context
) {
118 let bro
= context
.environment
.chromeWindow
.gBrowser
;
121 case "credits": list
= "CREDITS"; break;
122 case "thanks": list
= "THANKS"; break;
123 case "licenses": list
= ["LICENSE.bsd", "LICENSE.mit", "LICENSE.mpl", "LICENSE.wtfpl"]; break;
124 default: list
= "README"; break;
126 if (typeof(list
) == "string") {
127 bro
.selectedTab
= bro
.addTab("chrome://guerilla/content/"+list
);
129 for (let name
of list
) bro
.selectedTab
= bro
.addTab("chrome://guerilla/content/"+name
);
135 name
: "guerilla reset",
136 description
: "reset all internal caches",
137 exec: function (args
, context
) {
138 //conlog("clearing guerilla caches...");
144 name
: "guerilla debug",
145 description
: "switch debug mode on/off",
148 description
: "action to perform",
149 type
: { name
: "selection", data
: ["show", "tan", "ona", "toggle"] },
150 defaultValue
: "show",
153 returnValue
: "string",
154 exec: function (args
, context
) {
156 case "tan": guerillaOptions
.debugMode
= true; break;
157 case "ona": guerillaOptions
.debugMode
= false; break;
158 case "toggle": guerillaOptions
.debugMode
= !guerillaOptions
.debugMode
; break;
160 return "guerilla debug mode is "+(guerillaOptions
.debugMode
? "on" : "off");
165 name
: "guerilla log",
166 description
: "switch logging on/off",
169 description
: "action to perform",
170 type
: { name
: "selection", data
: ["show", "tan", "ona", "toggle"] },
171 defaultValue
: "show",
174 returnValue
: "string",
175 exec: function (args
, context
) {
177 case "tan": guerillaOptions
.logEnabled
= true; break;
178 case "ona": guerillaOptions
.logEnabled
= false; break;
179 case "toggle": guerillaOptions
.logEnabled
= !guerillaOptions
.logEnabled
; break;
181 return "guerilla logging is "+(guerillaOptions
.logEnabled
? "on" : "off");
186 name
: "guerilla state",
187 description
: "switch guerilla state",
190 description
: "action to perform",
191 type
: { name
: "selection", data
: ["show", "active", "inactive", "toggle"] },
192 defaultValue
: "show",
195 returnValue
: "string",
196 exec: function (args
, context
) {
198 case "active": guerillaOptions
.active
= true; break;
199 case "inactive": guerillaOptions
.active
= false; break;
200 case "toggle": guerillaOptions
.active
= !guerillaOptions
.active
; break;
202 return "guerilla is "+(guerillaOptions
.active
? "" : "in")+"active";
207 name
: "guerilla activate",
208 description
: "activate guerilla",
209 returnValue
: "string",
210 exec: function (args
, context
) {
211 guerillaOptions
.active
= true;
212 return "guerilla is active";
217 name
: "guerilla deactivate",
218 description
: "deactivate guerilla",
219 returnValue
: "string",
220 exec: function (args
, context
) {
221 guerillaOptions
.active
= false;
222 return "guerilla is inactive";
227 name
: "guerilla new",
228 description
: "create new userscript",
231 description
: "new script name",
235 exec: function (args
, context
) {
236 let mt
= args
.filename
.match(/^\/?lib\/([^\/]+)$/);
240 dir
= getUserLibDir();
241 if (!dir
.exists()) dir
.create(res
.DIRECTORY_TYPE
, 0750);
242 dir
.append(addjsext(mt
[1]));
244 if (args
.filename
.length
== 0 || args
.filename
.indexOf("/") >= 0) { alert("invalid file name: "+args
.filename
); return; }
245 dir
= getUserJSDir();
246 dir
.append(addjsext(args
.filename
));
248 conlog("trying to edit '"+dir
.path
+"'");
249 openEditor(dir
.path
, context
.environment
.chromeDocument
.defaultView
, true);
254 name
: "guerilla edit",
255 description
: "edit guerilla script",
256 //returnValue: "string",
259 description
: "script name to edit",
263 lookup: function (context
) {
264 // recache scripts if necessary
265 let uc
= scacheAPI
.getUC();
266 if (latestUC
=== false || latestUC
!= uc
) {
268 scacheAPI
.validate();
269 latestUC
= scacheAPI
.getUC();
270 buildFileList(scacheAPI
.getScripts());
277 exec: function (args
, context
) {
278 if (args
.filename
) openEditor(args
.filename
.path
, context
.environment
.chromeDocument
.defaultView
);
284 exports
.gcliInit = function () {
285 for (let f
= 0; f
< gcliCommandSpecs
.length
; ++f
) {
286 if (gcliCommandSpecs
[f
].name
) gcli
.addCommand(gcliCommandSpecs
[f
]);
291 exports
.gcliDeinit = function () {
292 for (let f
= 0; f
< gcliCommandSpecs
.length
; ++f
) {
293 if (gcliCommandSpecs
[f
].name
) gcli
.removeCommand(gcliCommandSpecs
[f
]);