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
189
190
191
192
193
194
195
196
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { setTimeout } from "resource://gre/modules/Timer.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
// Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
// See LOG_LEVELS in Console.sys.mjs. Common examples: "All", "Info", "Warn", &
// "Error".
const PREF_LOG_LEVEL = "extensions.mozscreenshots@mozilla.org.loglevel";
const lazy = {};
XPCOMUtils.defineLazyGetter(lazy, "log", () => {
let { ConsoleAPI } = ChromeUtils.importESModule(
"resource://gre/modules/Console.sys.mjs"
);
let consoleOptions = {
maxLogLevel: "info",
maxLogLevelPref: PREF_LOG_LEVEL,
prefix: "mozscreenshots",
};
return new ConsoleAPI(consoleOptions);
});
export var Screenshot = {
_extensionPath: null,
_path: null,
_imagePrefix: "",
_imageExtension: ".png",
_screenshotFunction: null,
init(path, extensionPath, imagePrefix = "") {
this._path = path;
let dir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
dir.initWithPath(this._path);
if (!dir.exists()) {
dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
}
this._extensionPath = extensionPath;
this._imagePrefix = imagePrefix;
switch (Services.appinfo.OS) {
case "WINNT":
this._screenshotFunction = this._screenshotWindows;
break;
case "Darwin":
this._screenshotFunction = this._screenshotOSX;
break;
case "Linux":
this._screenshotFunction = this._screenshotLinux;
break;
default:
throw new Error("Unsupported operating system");
}
},
_buildImagePath(baseName) {
return PathUtils.join(
this._path,
this._imagePrefix + baseName + this._imageExtension
);
},
// Capture the whole screen using an external application.
async captureExternal(filename) {
let imagePath = this._buildImagePath(filename);
await this._screenshotFunction(imagePath);
lazy.log.debug("saved screenshot: " + filename);
return imagePath;
},
// helpers
_screenshotWindows(filename) {
return new Promise((resolve, reject) => {
let exe = Services.dirsvc.get("GreBinD", Ci.nsIFile);
exe.append("screenshot.exe");
if (!exe.exists()) {
exe = Services.dirsvc.get("CurWorkD", Ci.nsIFile).parent;
exe.append("bin");
exe.append("screenshot.exe");
}
let process = Cc["@mozilla.org/process/util;1"].createInstance(
Ci.nsIProcess
);
process.init(exe);
let args = [filename];
process.runAsync(
args,
args.length,
this._processObserver(resolve, reject)
);
});
},
async _screenshotOSX(filename) {
let screencapture = (windowID = null) => {
return new Promise((resolve, reject) => {
// Get the screencapture executable
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
file.initWithPath("/usr/sbin/screencapture");
let process = Cc["@mozilla.org/process/util;1"].createInstance(
Ci.nsIProcess
);
process.init(file);
// Run the process.
let args = ["-x", "-t", "png"];
args.push(filename);
process.runAsync(
args,
args.length,
this._processObserver(resolve, reject)
);
});
};
function readWindowID() {
return IOUtils.readUTF8("/tmp/mozscreenshots-windowid");
}
let promiseWindowID = () => {
return new Promise((resolve, reject) => {
// Get the window ID of the application (assuming its front-most)
let osascript = Cc["@mozilla.org/file/local;1"].createInstance(
Ci.nsIFile
);
osascript.initWithPath("/bin/bash");
let osascriptP = Cc["@mozilla.org/process/util;1"].createInstance(
Ci.nsIProcess
);
osascriptP.init(osascript);
let osaArgs = [
"-c",
"/usr/bin/osascript -e 'tell application (path to frontmost application as text) to set winID to id of window 1' > /tmp/mozscreenshots-windowid",
];
osascriptP.runAsync(
osaArgs,
osaArgs.length,
this._processObserver(resolve, reject)
);
});
};
await promiseWindowID();
let windowID = await readWindowID();
await screencapture(windowID);
},
_screenshotLinux(filename) {
return new Promise((resolve, reject) => {
let exe = Services.dirsvc.get("GreBinD", Ci.nsIFile);
exe.append("screentopng");
if (!exe.exists()) {
exe = Services.dirsvc.get("CurWorkD", Ci.nsIFile).parent;
exe.append("bin");
exe.append("screentopng");
}
let process = Cc["@mozilla.org/process/util;1"].createInstance(
Ci.nsIProcess
);
process.init(exe);
let args = [filename];
process.runAsync(
args,
args.length,
this._processObserver(resolve, reject)
);
});
},
_processObserver(resolve, reject) {
return {
observe(subject, topic, data) {
switch (topic) {
case "process-finished":
try {
// Wait 1s after process to resolve
setTimeout(resolve, 1000);
} catch (ex) {
reject(ex);
}
break;
default:
reject(topic);
break;
}
},
};
},
};
|