summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/viewSource.js
blob: 8fd3a9ccde8e8cf88e3c33575522adf8a884c499 (plain)
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
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-

/* 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 gViewSourceUtils, internalSave, ZoomManager */

var { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);

XPCOMUtils.defineLazyScriptGetter(
  this,
  "PrintUtils",
  "chrome://messenger/content/printUtils.js"
);

// Needed for printing.
window.browserDOMWindow = window.opener.browserDOMWindow;

var gBrowser;
addEventListener("load", () => {
  gBrowser = document.getElementById("content");
  gBrowser.getTabForBrowser = () => {
    return null;
  };
  gBrowser.addEventListener("pagetitlechanged", () => {
    document.title =
      document.documentElement.getAttribute("titlepreface") +
      gBrowser.contentTitle +
      document.documentElement.getAttribute("titlemenuseparator") +
      document.documentElement.getAttribute("titlemodifier");
  });

  if (Services.prefs.getBoolPref("view_source.wrap_long_lines", false)) {
    document
      .getElementById("cmd_wrapLongLines")
      .setAttribute("checked", "true");
  }

  gViewSourceUtils.viewSourceInBrowser({
    ...window.arguments[0],
    viewSourceBrowser: gBrowser,
  });
  gBrowser.contentWindow.focus();

  document
    .getElementById("repair-text-encoding")
    .setAttribute("disabled", !gBrowser.mayEnableCharacterEncodingMenu);
  gBrowser.addEventListener(
    "load",
    () => {
      document
        .getElementById("repair-text-encoding")
        .setAttribute("disabled", !gBrowser.mayEnableCharacterEncodingMenu);
    },
    true
  );

  gBrowser.addEventListener(
    "DoZoomEnlargeBy10",
    () => {
      ZoomManager.scrollZoomEnlarge(gBrowser);
    },
    true
  );
  gBrowser.addEventListener(
    "DoZoomReduceBy10",
    () => {
      ZoomManager.scrollReduceEnlarge(gBrowser);
    },
    true
  );
});

var viewSourceChrome = {
  promptAndGoToLine() {
    let actor = gViewSourceUtils.getViewSourceActor(gBrowser.browsingContext);
    actor.manager.getActor("ViewSourcePage").promptAndGoToLine();
  },

  toggleWrapping() {
    let state = gBrowser.contentDocument.body.classList.toggle("wrap");
    if (state) {
      document
        .getElementById("cmd_wrapLongLines")
        .setAttribute("checked", "true");
    } else {
      document.getElementById("cmd_wrapLongLines").removeAttribute("checked");
    }
    Services.prefs.setBoolPref("view_source.wrap_long_lines", state);
  },

  /**
   * Called by clicks on a menuitem to force the character set detection.
   */
  onForceCharacterSet() {
    gBrowser.forceEncodingDetection();
    gBrowser.reloadWithFlags(Ci.nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
  },

  /**
   * Reloads the browser, bypassing the network cache.
   */
  reload() {
    gBrowser.reloadWithFlags(
      Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY |
        Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE
    );
  },
};

// viewZoomOverlay.js uses this
function getBrowser() {
  return gBrowser;
}

// Strips the |view-source:| for internalSave()
function ViewSourceSavePage() {
  internalSave(
    gBrowser.currentURI.spec.replace(/^view-source:/i, ""),
    null,
    null,
    null,
    null,
    null,
    null,
    "SaveLinkTitle",
    null,
    null,
    gBrowser.cookieJarSettings,
    gBrowser.contentDocument,
    null,
    gBrowser.webNavigation.QueryInterface(Ci.nsIWebPageDescriptor),
    null,
    Services.scriptSecurityManager.getSystemPrincipal()
  );
}

/** Called by ContextMenuParent.sys.mjs */
function openContextMenu({ data }, browser, actor) {
  let popup = browser.ownerDocument.getElementById("viewSourceContextMenu");

  let newEvent = document.createEvent("MouseEvent");
  let screenX = data.context.screenXDevPx / window.devicePixelRatio;
  let screenY = data.context.screenYDevPx / window.devicePixelRatio;
  newEvent.initNSMouseEvent(
    "contextmenu",
    true,
    true,
    null,
    0,
    screenX,
    screenY,
    0,
    0,
    false,
    false,
    false,
    false,
    2,
    null,
    0,
    data.context.mozInputSource
  );
  popup.openPopupAtScreen(newEvent.screenX, newEvent.screenY, true, newEvent);
}