forked from LouCypher/mozcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view-source.mozcmd
62 lines (56 loc) · 2.05 KB
/
view-source.mozcmd
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
/*
* 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/.
*
* Contributor(s):
* - LouCypher (original code)
*/
[
{
name: "vs",
description: "View HTML source of current page or a specified URL.",
runAt: "client",
params: [
{
name: "url",
type: "string",
defaultValue: null,
description: "Web page URL to view its source. If no URL is entered,\
view source of current page."
}
],
exec: function(args, context) {
let environment = context.environment;
let contentDoc = environment.document || environment.contentDocument;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;
let url;
if (args.url)
url = args.url;
else
url = contentDoc.location.href;
if (!/^[a-z0-9]+:/i.test(url))
url = "http://" + url; // Use 'http' by default
chromeWin.switchToTabHavingURI("view-source:" + url, true);
}
},
{
name: "vrs",
description: "View rendered source of current page.",
runAt: "client",
exec: function(args, context) {
var Cc = Components.classes; var Ci = Components.interfaces;
let environment = context.environment;
let contentDoc = environment.document || environment.contentDocument;
let chromeWin = environment.chromeWindow || environment.chromeDocument.defaultView;
let domSerializer = Cc["@mozilla.org/xmlextras/xmlserializer;1"].
createInstance(Ci.nsIDOMSerializer);
let source = domSerializer.serializeToString(contentDoc);
let isHTML = contentDoc.createElement("div").tagName === "DIV";
let contentType = isHTML ? "text/html" : "application/xml";
let dataURI = "data:" + contentType + ";charset=utf-8" + ","
+ encodeURIComponent(source);
chromeWin.switchToTabHavingURI("view-source:" + dataURI, true);
}
}
]