-
Notifications
You must be signed in to change notification settings - Fork 0
/
ucb_SaveHTML.mjs
339 lines (335 loc) · 15 KB
/
ucb_SaveHTML.mjs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* SingleHtml Ff103+ © Лекс, правка Dumby, mod Dobrov +глобальные функции
вызов: Cu.getGlobalForObject(Cu)[Symbol.for("UcfGlob")].SingleHTML(1) если 0: выбор пути */
var self, name = "SingleHTML", EXPORTED_SYMBOLS = [`${name}Child`], {io, focus, obs, prefs, dirsvc} = Services;
export class SingleHTMLChild extends JSWindowActorChild { //класс = name + Child
receiveMessage() {return htmlAndName(this.contentWindow);}
}
ChromeUtils.domProcessChild.childID || ({
init(topic) {
ChromeUtils.registerWindowActor(name, {
allFrames: true,
child: {esModuleURI: Components.stack.filename},
messageManagerGroups: ["browsers"]
});
obs.addObserver(self = this, topic);
obs.addObserver(function quit(s, t) {
obs.removeObserver(quit, t);
obs.removeObserver(self, topic);
}, "quit-application-granted");
this.handleEvent = e => this[e.type](e);
globalThis[Symbol.for('UcfGlob')] = this.UcfGlob; //общие функции
},
observe(win) {
win.document.getElementById("appMenu-popup").addEventListener("popupshowing", this);
win.addEventListener("unload", this);
},
popupshowing(e) {
this.unload(e);
var popup = e.target;
var btn = popup.ownerDocument.createXULElement("toolbarbutton");
btn.id = "appMenu-ucf-save-html-button";
btn.setAttribute("label", "Всё или выбранное в единый HTML");
var before = "appMenu-save-file-button2", subviewbutton = "subviewbutton";
btn.className = subviewbutton;
btn.setAttribute("oncommand", "SingleHTML();");
btn.SingleHTML = this.UcfGlob.SingleHTML;
popup.querySelector('toolbarbutton[id^="'+ before +'"]').before(btn);
},
unload(e) {
var win = e.target.ownerGlobal;
win.removeEventListener("unload", this);
win.document.getElementById("appMenu-popup").removeEventListener("popupshowing", this);
},
get win(){
return this.ownerGlobal || Services.wm.getMostRecentWindow("navigator:browser");
},
async save(win, data, fname, host, to) {
var path = self.UcfGlob.TitlePath(to, fname, host, win); //путь в зависимости от опций
var dir = path[0], t = path[2].slice(0,48), path = path[1];
try {dir.exists() && dir.isDirectory() || dir.create(dir.DIRECTORY_TYPE, 0o777);}
catch(ex){
self.UcfGlob.Succes(dir.path, 0); return;}
if (!to){ // диалог выбора папки
var fp = Cc['@mozilla.org/filepicker;1'].createInstance(Ci.nsIFilePicker);
fp.init(parseInt(Services.appinfo.platformVersion) < 125 ? win : win.browsingContext,"", fp.modeSave);
fp.defaultString = path.split(/.*[\/|\\]/)[1];
fp.displayDirectory = dir;
fp.appendFilters(fp.filterHTML); fp.appendFilters(fp.filterAll);
var res = await new Promise(fp.open);
if (res == fp.returnOK || res == fp.returnReplace)
path = fp.file.path
else return;
}; to = 1;
try {await IOUtils.writeUTF8(path, data);} catch {to = 0}
await self.UcfGlob.Succes(path, to, '√ страница записана: '+ t);
},
UcfGlob: {
async restart(){
var meth = Services.appinfo.inSafeMode ? "restartInSafeMode" : "quit";
Services.startup[meth](Ci.nsIAppStartup.eAttemptQuit | Ci.nsIAppStartup.eRestart);
},
maybeRestart(conf, exe = this.restart, lab = Services.appinfo.vendor){
if (conf && !Services.prompt.confirm(null, lab, "Перезапустить браузер?")) return;
let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
Services.obs.notifyObservers(cancel, "quit-application-requested", "restart");
return cancel.data ? Services.prompt.alert(null, lab, "Запрос на выход отменён.") : exe();
},
Pref(key,set){ //или key = [key,default]
if(!Array.isArray(key)) key = [key];
var t = prefs.getPrefType(key[0]), m = {b:"Bool",n:"Int",s:"String"};
t = m[t == 128 ? "b" : t == 64 ? "n" : t == 32 ? "s" : ""];
if(set == "get") return t; //тип опции
if(!t) t = m[set != undefined ? (typeof set)[0] : (typeof key[1])[0]];
if(t) if(set != undefined)
prefs[`set${t}Pref`](key[0],set)
else set = prefs[`get${t}Pref`](...key);
return set;
},
dirGet(){ //dir [, subdirs]. last arg ? 1 ret path : open
var f, d = [...arguments], c = Ci.nsIFile, e = "DfltDwnld", r = (d[d.length-1] == 1);
try {var b = prefs.getComplexValue("browser.download.dir",c);} catch {b = dirsvc.get(e,c)}
(r) && d.pop(); if (d[0] == e) f = b;
else try {f = dirsvc.get(d[0], c);} catch{f = b}
d.slice(1, d.length).forEach((c) => f.append(c));
if(r) return f.path; f.exists() && f.launch();
},
async Status(text,time, win = self.win){
var StatusPanel = win.StatusPanel;
if(StatusPanel.update.tid)
win.clearTimeout(StatusPanel.update.tid)
else {
var {update} = StatusPanel;
StatusPanel.update = () => {};
StatusPanel.update.ret = () => {
StatusPanel.update = update,StatusPanel.update();
}}
StatusPanel.update.tid = win.setTimeout(StatusPanel.update.ret,time || 5e3);
StatusPanel._label = text;
},
Flash(id,color = 'rgba(0,200,0,0.3)',style,txt,time,ms = 350,win = self.win){
id = win.document.getElementById(id || 'urlbar-input-container');
id &&= id.style; if(isNaN(Number(txt)))
this.Status(txt,time); //мигание, статус
if(style && id) id.filter = style;
if(color && id) if(txt < 0)
id.fill = color; else id.background = color;
if(!(txt < 0) && ms && id) win.setTimeout(
()=> ["filter","background","fill"].forEach((c)=> id.removeProperty(c)), ms);
},
async Succes(path, w = 1, text, win = self.win, s,i){
this.Flash(0, w ? undefined : 'rgba(250,0,0,0.2)',0, w ? text : 0);
if(!w) return; s = `${w == 1 ? "und" : "ov"}erline`;
i = win.gBrowser.selectedTab.textLabel.style;
if(!i.textDecoration.includes(s)) i.textDecoration = i.textDecoration +" "+ s;
if(w != 1) return; //ClickPicSave, иначе FakeDownload
w = await win.Downloads.createDownload({source: "about:blank",target: win.FileUtils.File(path)});
(await win.Downloads.getList(win.Downloads.ALL)).add(w);
await w.refresh(w.succeeded = true);
},
async SingleHTML(to = false, win = self.win){
var br = win.gBrowser.selectedBrowser, bc = focus.focusedContentBrowsingContext;
if (bc?.top.embedderElement != br) bc = br.browsingContext;
var actor = bc?.currentWindowGlobal?.getActor(name);
actor && self.save(win, ...await actor.sendQuery(""), to); //htmlAndName
},
TitlePath(to, d, h, win = self.win, n = 0, u = 99){ //0 web|2 pic|-№ cut, name, url
if(parseInt(to) > 0) [n,to] = [to,n]; if(parseInt(to) < 0) u = Math.abs(to);
if(typeof(to) != 'string' || !/.*\|/.test(to))
to = prefs.getStringPref("extensions.user_chrome_files.savedirs","|||0");
to = to.split('|').slice(0 + n, 2 + n); //Dir/Sub|[empty|0 title|1 url]
d = /^blank/.test(d || "blank") ? win.gBrowser.selectedTab.label : d;
d = d.replace(/\s+/g,' ').replace(/:/g,'։').replace(/[|<>]+/g,'_').replace(/([\\\/?*\"'`]+| ։։ .*)/g,'').slice(0,u).trim();
h ||= decodeURIComponent(win.gURLBar.value); n = d, u = h;
h = /^file:\/\//.test(h) ? 'file' : h.replace(/^.*u=|https?:\/\/|www\.|\/.*/g,'').replace(/^(moz-extension|ru\.|m\.)/,'').replace(/\/.*/,'');
to[1] = (to[1] == "0") ? d : (to[1] == "1") ? h : "";
d += "_"+ new Date().toLocaleDateString('ru', {day: 'numeric',month: 'numeric',year: '2-digit'}) +'-'+ new Date().toLocaleTimeString('en-GB').replace(/:/g,"։"); //дата-часы
try {var dir = prefs.getComplexValue("browser.download.dir",Ci.nsIFile);} catch {dir = dirsvc.get("DfltDwnld",Ci.nsIFile)}
var map = s => win.DownloadPaths.sanitize(s); //FIX имён
to.map(map).forEach(dir.append);
to = dir.clone(); to.append(d +'.html');
return [dir, to.path, n, d, u, h]; //… имя, +дата, URL, домен
},
FileOk(path, win = self.win){ //файл|папка есть?
if(path.startsWith("chrome://"))
path = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIXULChromeRegistry).convertChromeURL(io.newURI(path)).spec;
path = path.replace(/.+?:\/\//,"").replace(/%20/g," ");
if (win.AppConstants.platform == "win"){
path = path.replace(/^\/?/,"").replace(/\//g,"\\\\");}
try {return win.FileUtils.File(String.raw`${path}`).exists();
} catch {}; return false;
},
RunwA(){let args = [...arguments], path = args.shift(), file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile), proc = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
file.initWithPath(path);
try{proc.init(file);} catch{console.error(F.q + path); return 1;}
proc.runwAsync(args, args.length);
}
}
}).init("browser-delayed-startup-finished");
var htmlAndName = async mainWin => { //meteo7.ru не сохраняет SVG
var resolveURL = function (url, base) {
try { return io.newURI(url, null, io.newURI(base)).spec;} catch {}
},
getSelWin = function (w) {
if (w.getSelection().toString()) return w;
for (var i = 0, f, r; f = w.frames[i]; i++) {
try { if (r = getSelWin(f)) return r;} catch {}
}
},
encodeImg = function (src, obj, canvas, img) {
if (/^https?:\/\//.test(src)) {
canvas = doc.createElement('canvas');
if (!obj || obj.nodeName.toLowerCase() != 'img') {
img = doc.createElement('img');
img.src = src;
} else
img = obj;
if (img.complete) try {
canvas.width = img.naturalWidth || img.width || 128;
canvas.height = img.naturalHeight || img.height || 128;
canvas.getContext('2d').drawImage(img, 0, 0, canvas.width, canvas.height);
src = canvas.toDataURL(/\.jpe?g/i.test(src) ? 'image/jpeg' : 'image/png'); //\.svg/i.test(src) ? 'image/svg+xml'
} catch {};
if (img != obj) img.src = 'about:blank';
};
return src;
},
toSrc = function (obj) {
var strToSrc = function (str) {
var chr, ret = '', i = 0, meta = {'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','\x22':'\\\x22','\\':'\\\\'};
while (chr = str.charAt(i++)) {
ret += meta[chr] || chr;
};
return '\x22' + ret + '\x22';
},
arrToSrc = function (arr) {
var ret = [];
for (var i = 0; i < arr.length; i++) {
ret[i] = toSrc(arr[i]) || 'null';
};
return '[' + ret.join(',') + ']';
},
objToSrc = function (obj) {
var val, ret = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop) && (val = toSrc(obj[prop]))) ret.push(strToSrc(prop) + ': ' + val);
};
return '{' + ret.join(',') + '}';
};
switch (Object.prototype.toString.call(obj).slice(8, -1)) {
case 'Array': return arrToSrc(obj);
case 'Boolean':
case 'Function':
case 'RegExp': return obj.toString();
case 'Date': return 'new Date(' + obj.getTime() + ')';
case 'Math': return 'Math';
case 'Number': return isFinite(obj) ? String(obj) : 'null';
case 'Object': return objToSrc(obj);
case 'String': return strToSrc(obj);
default: return obj ? (obj.nodeType == 1 && obj.id ? 'document.getElementById(' + strToSrc(obj.id) + ')' : '{}') : 'null';
}
},
selWin = getSelWin(mainWin), win = selWin || mainWin, doc = win.document, loc = win.location,
ele, pEle, clone, reUrl = /(url\(\x22)(.+?)(\x22\))/g;
if (selWin) {
var rng = win.getSelection().getRangeAt(0);
pEle = rng.commonAncestorContainer;
ele = rng.cloneContents();
} else {
pEle = doc.documentElement;
ele = (doc.body || doc.getElementsByTagName('body')[0]).cloneNode(true);
};
while (pEle) {
if (pEle.nodeType == 1) {
clone = pEle.cloneNode(false);
clone.appendChild(ele);
ele = clone;
};
pEle = pEle.parentNode
};
var sel = doc.createElement('div');
sel.appendChild(ele);
for (var el, all = sel.getElementsByTagName('*'), i = all.length; i--;) {
el = all[i];
if (el.style && el.style.backgroundImage) el.style.backgroundImage = el.style.backgroundImage.replace(reUrl, function (a, prev, url, next) {
if (!/^[a-z]+:/.test(url)) url = resolveURL(url, loc.href);
return prev + encodeImg(url) + next;
});
switch (el.nodeName.toLowerCase()) {
case 'link':
case 'style':
case 'script': el.parentNode.removeChild(el); break;
case 'a':
case 'area': if (el.hasAttribute('href') && el.getAttribute('href').charAt(0) != '#') el.href = el.href; break;
case 'img':
case 'input': if (el.hasAttribute('src')) el.src = encodeImg(el.src, el); break;
case 'audio':
case 'video':
case 'embed':
case 'frame':
case 'iframe': if (el.hasAttribute('src')) el.src = el.src; break;
case 'object': if (el.hasAttribute('data')) el.data = el.data; break;
case 'form': if (el.hasAttribute('action')) el.action = el.action; break;
}
};
var head = ele.insertBefore(doc.createElement('head'), ele.firstChild), meta = doc.createElement('meta'), sheets = doc.styleSheets;
meta.httpEquiv = 'content-type';
meta.content = 'text/html; charset=utf-8';
head.appendChild(meta);
var title = doc.getElementsByTagName('title')[0];
title && head.appendChild(title.cloneNode(true));
head.copyScript = function (unsafeWin) {
if ('$' in unsafeWin) return;
var f = doc.createElement('iframe');
f.src = 'about:blank';
f.setAttribute('style', 'position:fixed;left:0;top:0;visibility:hidden;width:0;height:0;');
doc.documentElement.appendChild(f);
var str, script = doc.createElement('script');
script.type = 'text/javascript';
for (var name in unsafeWin) {
if (name in f.contentWindow || !/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(name)) continue;
try {
str = toSrc(unsafeWin[name]);
if (!/\{\s*\[native code\]\s*\}/.test(str)) {
script.appendChild(doc.createTextNode('var ' + name + ' = ' + str.replace(/<\/(script>)/ig, '<\\/$1') + ';\n'));
}
} catch {};
};
f.parentNode.removeChild(f);
if (script.childNodes.length) this.nextSibling.appendChild(script);
};
head.copyScript(win.wrappedJSObject || win);
head.copyStyle = function (s) {
if (!s) return;
var style = doc.createElement('style');
style.type = 'text/css';
if (s.media && s.media.mediaText) style.media = s.media.mediaText;
try {
for (var i = 0, rule; rule = s.cssRules[i]; i++) {
if (rule.type != 3) {
if((!rule.selectorText || rule.selectorText.indexOf(':') != -1) || (!sel.querySelector || sel.querySelector(rule.selectorText))) {
var css = !rule.cssText ? '' : rule.cssText.replace(reUrl, function (a, prev, url, next) {
if (!/^[a-z]+:/.test(url)) url = resolveURL(url, s.href || loc.href);
if(rule.type == 1 && rule.style && rule.style.backgroundImage) url = encodeImg(url);
return prev + url + next;
});
style.appendChild(doc.createTextNode(css + '\n'));
}
} else { this.copyStyle(rule.styleSheet);}
}
} catch {
if (s.ownerNode) style = s.ownerNode.cloneNode(false);
};
this.appendChild(style);
};
for (var j = 0; j < sheets.length; j++) head.copyStyle(sheets[j]);
head.appendChild(doc.createTextNode('\n'));
var doctype = '', dt = doc.doctype;
if (dt && dt.name) {
doctype += '<!DOCTYPE ' + dt.name;
if (dt.publicId) doctype += ' PUBLIC \x22' + dt.publicId + '\x22';
if (dt.systemId) doctype += ' \x22' + dt.systemId + '\x22';
doctype += '>\n';
};
var onlyName = selWin ? win.getSelection().toString() : (title && title.text ? title.text : loc.pathname.split('/').pop());
return [doctype + sel.innerHTML +'\n<a href='+ (loc.protocol != 'data:' ? loc.href : 'data:uri') +'><small><blockquote>источник: '+ new Date().toLocaleString("ru") +'</blockquote></small></a>', onlyName, loc.hostname];
}