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
|
/* 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/. */
/* globals global, documentMetadata, util, uicontrol, ui, catcher */
/* globals buildSettings, domainFromUrl, randomString, shot, blobConverters */
"use strict";
this.shooter = (function() { // eslint-disable-line no-unused-vars
const exports = {};
const { AbstractShot } = shot;
const RANDOM_STRING_LENGTH = 16;
const MAX_CANVAS_DIMENSION = 32767;
let backend;
let shotObject;
const callBackground = global.callBackground;
const clipboard = global.clipboard;
function regexpEscape(str) {
// http://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
function sanitizeError(data) {
const href = new RegExp(regexpEscape(window.location.href), "g");
const origin = new RegExp(`${regexpEscape(window.location.origin)}[^ \t\n\r",>]*`, "g");
const json = JSON.stringify(data)
.replace(href, "REDACTED_HREF")
.replace(origin, "REDACTED_URL");
const result = JSON.parse(json);
return result;
}
catcher.registerHandler((errorObj) => {
callBackground("reportError", sanitizeError(errorObj));
});
function hideUIFrame() {
ui.iframe.hide();
return Promise.resolve(null);
}
function screenshotPage(dataUrl, selectedPos, type, screenshotTaskFn) {
let promise = Promise.resolve(dataUrl);
if (!dataUrl) {
let isFullPage = type === "fullPage" || type == "fullPageTruncated";
promise = callBackground(
"screenshotPage",
selectedPos.toJSON(),
isFullPage,
window.devicePixelRatio);
}
catcher.watchPromise(promise.then((dataUrl) => {
screenshotTaskFn(dataUrl);
}));
}
exports.downloadShot = function(selectedPos, previewDataUrl, type) {
const shotPromise = previewDataUrl ? Promise.resolve(previewDataUrl) : hideUIFrame();
catcher.watchPromise(shotPromise.then(dataUrl => {
screenshotPage(dataUrl, selectedPos, type, url => {
let type = blobConverters.getTypeFromDataUrl(url);
type = type ? type.split("/", 2)[1] : null;
shotObject.delAllClips();
shotObject.addClip({
createdDate: Date.now(),
image: {
url,
type,
location: selectedPos,
},
});
ui.triggerDownload(url, shotObject.filename);
uicontrol.deactivate();
});
}));
};
exports.preview = function(selectedPos, type) {
catcher.watchPromise(hideUIFrame().then(dataUrl => {
screenshotPage(dataUrl, selectedPos, type, url => {
ui.iframe.usePreview();
ui.Preview.display(url);
});
}));
};
let copyInProgress = null;
exports.copyShot = function(selectedPos, previewDataUrl, type) {
// This is pretty slow. We'll ignore additional user triggered copy events
// while it is in progress.
if (copyInProgress) {
return;
}
// A max of five seconds in case some error occurs.
copyInProgress = setTimeout(() => {
copyInProgress = null;
}, 5000);
const unsetCopyInProgress = () => {
if (copyInProgress) {
clearTimeout(copyInProgress);
copyInProgress = null;
}
};
const shotPromise = previewDataUrl ? Promise.resolve(previewDataUrl) : hideUIFrame();
catcher.watchPromise(shotPromise.then(dataUrl => {
screenshotPage(dataUrl, selectedPos, type, url => {
const blob = blobConverters.dataUrlToBlob(url);
catcher.watchPromise(callBackground("copyShotToClipboard", blob).then(() => {
uicontrol.deactivate();
unsetCopyInProgress();
}, unsetCopyInProgress));
});
}));
};
exports.sendEvent = function(...args) {
const maybeOptions = args[args.length - 1];
if (typeof maybeOptions === "object") {
maybeOptions.incognito = browser.extension.inIncognitoContext;
} else {
args.push({incognito: browser.extension.inIncognitoContext});
}
callBackground("sendEvent", ...args);
};
catcher.watchFunction(() => {
shotObject = new AbstractShot(
backend,
randomString(RANDOM_STRING_LENGTH) + "/" + domainFromUrl(location),
{
origin: shot.originFromUrl(location.href),
}
);
shotObject.update(documentMetadata());
})();
return exports;
})();
null;
|