2 The contents of this file are subject to the Mozilla Public
3 License Version 1.1 (the "License"); you may not use this file
4 except in compliance with the License. You may obtain a copy of
5 the License at http://www.mozilla.org/MPL/
7 Software distributed under the License is distributed on an "AS
8 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 implied. See the License for the specific language governing
10 rights and limitations under the License.
12 fehmozexRunProgram and fehmozexGetEnvironment are taken from MozEx (and modified).
13 MozEx is Copyright (C) 2003 Tomas Styblo <tripie@cpan.org>
14 the stuff to read link-handlers.js cargo culted from AdBlock plus
15 The rest is by Larry D'Anna and are Copyright (C) 2007 Larry D'Anna <larry@elder-gods.org>
19 function fehError (msg
) {
23 function fehmozexGetEnvironment(key
) {
27 var pr
= Components
.classes
["@mozilla.org/process/util;1"].
28 createInstance(Components
.interfaces
.nsIProcess
);
29 var env
= Components
.classes
["@mozilla.org/process/environment;1"];
33 env
= env
.createInstance(Components
.interfaces
.nsIEnvironment
);
38 // Hack for mozilla 1.4
39 res
= pr
.getEnvironment(key
);
49 function fehmozexRunProgram(cmd
, esc
) {
50 if (cmd
== null || cmd
.length
== 0) {
51 return false; // no command is set
53 var args
= new Array();
56 // a) apos. followed by anything till apos. followed by space or end of string
57 // - escape apos. by backslash, if it is not supposed to be part of the string
58 // b) space delimited word
59 // - escape space by backslash, if it is not supposed to be part of the string
60 // No other escaping is implemented, because it would make writing windows-like
63 // Use debugging facility to see what is the result if you are not sure how
65 scmd
=cmd
.match(/"(\"|.)*?"(?=\s|$)|(\\\s|\S)+/g);
68 // If it is apos. delimited
69 if (scmd
[i
].slice(0,1)=='"')
71 // Remove quotes on the begin and end
72 scmd
[i
]=scmd
[i
].replace(/^"(.*?)"$/, "$1");
73 // De-escape apostroph
74 // \x22 is a " but the " is fucking up my emacs colors
75 scmd
[i
]=scmd
[i
].replace(/\\\x22/g, "\x22");
78 scmd
[i
]=scmd
[i
].replace(/\\(\s)/g, "$1");
82 var executable
= scmd
.shift();
83 if (executable
.length
== 0) {
84 fehError("no executable in command");
88 for (var i
= 0; i
< scmd
.length
; i
++) {
91 if (param
.length
== 0) {
94 for (var e
= 0; e
< param
.length
; e
++) {
98 if (esc
[a
] === undefined) {
99 fehError("unknown escape in command '" + cmd
+ "': %" + a
);
114 var exec
= Components
.classes
["@mozilla.org/file/local;1"].
115 createInstance(Components
.interfaces
.nsILocalFile
);
116 var pr
= Components
.classes
["@mozilla.org/process/util;1"].
117 createInstance(Components
.interfaces
.nsIProcess
);
120 // $PATH stuff (only on UNIX)
121 if (window
.navigator
.platform
.toLowerCase().indexOf("win") == -1)
123 path
= fehmozexGetEnvironment("PATH").split(":");
126 // If executable is an absolute path or we don not know how to get PATH
127 // (windows), run it or fail. Otherwise look for the executable in $PATH.
128 // FIXME: How do you tell portably if a path is absolute?
129 if (executable
.charAt(0) == "/" || path
== null) {
130 dump ("feh: executable is " + executable
+ "\n");
131 exec
.initWithPath(executable
);
132 if (! exec
.exists()) {
133 throw "executable '" + executable
+ "' does not exist.";
138 for (i
= 0; i
< path
.length
; i
++) {
139 exec
.initWithPath(path
[i
]);
140 exec
.appendRelativePath(executable
);
142 dump ("feh: executable is " + path
[i
] + "/" + executable
+ "\n");
148 throw "could not find '" + executable
+ "' in path:\n" + path
.join ("\n");
153 for (var j
= 0; j
< args
.length
; j
++)
154 dump ("feh: arg " + j
+ " is " + args
[j
] + "\n");
158 pr
.run(false /* don't block */, args
, args
.length
);
161 fehError("cannot run executable '" +
162 executable
+ "' (args: " + args
+ "): " + e
);
165 // alert(pr.pid); /* pid is not implemented :((( */
173 for (var name
in feh
.progs
) {
174 gContextMenu
.showItem( "context-" + name
, gContextMenu
.onLink
);
177 //gContextMenu.showItem( "context-za", gContextMenu.onLink );
178 //alert(">>>>>> " + gContextMenu.linkURL);
182 // initialization code
183 this.initialized
= true;
187 var dirService
= Components
.classes
["@mozilla.org/file/directory_service;1"].getService(Components
.interfaces
.nsIProperties
);
188 var path
= dirService
.get("ProfD", Components
.interfaces
.nsIFile
);
189 path
.append("link-handlers.js");
191 var stream
= Components
.classes
["@mozilla.org/network/file-input-stream;1"].createInstance(Components
.interfaces
.nsIFileInputStream
);
193 stream
.init(path
, 0x01, 0444, 0);
195 var sstream
= Components
.classes
["@mozilla.org/scriptableinputstream;1"]
196 .createInstance(Components
.interfaces
.nsIScriptableInputStream
);
197 sstream
.init(stream
);
201 var str
= sstream
.read(4096);
202 while (str
.length
> 0) {
204 str
= sstream
.read(4096);
212 progs
= eval("feh.progs = " + data
+ ";");
214 fehError("error reading link-handlers.js: " + e
);
218 var menu
= document
.getElementById("contentAreaContextMenu");
222 ni = document.createElement("menuitem");
223 ni.setAttribute("label", "feh");
224 ni.setAttribute("oncommand", "feh(this);");
225 menu.appendChild(ni);
227 ni = document.createElement("menuitem");
228 ni.setAttribute("label", "za");
229 ni.setAttribute("id", "context-za");
230 ni.setAttribute("oncommand", "feh.za(this)");
231 menu.appendChild(ni);
235 feh
.proga
= new Array();
238 for (var name
in feh
.progs
) {
239 dump("FEHHH " + name
+ " ::: " + feh
.progs
[name
] + "\n");
241 feh.progsubs[name] = function() {
242 fehmozexRunProgram (feh.progs[name], {'r' : gContextMenu.linkURL});
244 feh
.proga
.push (name
);
245 ni
= document
.createElement("menuitem");
246 ni
.setAttribute("label", name
);
247 ni
.setAttribute("id", "context-" + name
);
248 //ni.setAttribute("oncommand", "dump(\"ZZZZZZZZZZZZ +\"" + name + "\"\\n\"); feh.progsubs[\"" + name + "\"];");
249 //ni.setAttribute("oncommand", "dump(\"ZZZZZZZZZZZZ "+ name +"\\n\");");
250 dump("ZZZZZ: fehDispatch(\""+ name
+"\");\n");
251 //ni.setAttribute("oncommand", "fehDispatch(\""+ name +"\");");
252 ni
.setAttribute("oncommand", "fehDispatch("+ i
+");");
253 menu
.appendChild(ni
);
258 var h
= menu
.getAttribute("onpopupshowing");
259 menu
.feh_onshowing
= eval ("function(event) {" + h
+ "}");
262 menu.setAttribute("onpopupshowing",
263 "if (event.target != this) return true; gContextMenu = new nsContextMenu( this ); feh.onshow(); return gContextMenu.shouldDisplay;");*/
265 menu
.setAttribute("onpopupshowing", "var ret = this.feh_onshowing(event); feh.onshow(); return x;");
266 //alert(menu.initClipboardItems);
274 "seeurl" : "gnome-terminal -x sh -c \"echo foo: %r | less\" ",
275 "links" : "gnome-terminal -x links %r " },
281 fehmozexRunProgram ("gnome-terminal -x sh -c \"echo foo: %r | less\" ", {'r' : gContextMenu
.linkURL
});
287 function fehDispatch(i
) {
288 var name
= feh
.proga
[i
];
289 fehmozexRunProgram (feh
.progs
[name
], {'r' : gContextMenu
.linkURL
});
295 alert("feh " + o.parentNode.localName + ":::" + o.parentNode.getAttribute("onpopupshowing"));
300 window
.addEventListener("load", function(e
) { feh
.onLoad(e
); }, false);
This page took 0.460838 seconds and 4 git commands to generate.