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
|
/* vim: set expandtab tabstop=2 shiftwidth=2 softtabstop=2 foldmethod=marker: */
/**
* HTTPS Everywhere Firefox Extension: https://www.eff.org/https-everywhere/
*
* Licensed under the GPL v3+.
*
* @copyright Copyright (C) 2010-2013 Mike Perry <mikeperry@fscked.org>
* Peter Eckersley <pde@eff.org>
* and many others.
*/
// Define https everywhere variable object that will act as a namespace, so that
// global namespace pollution is avoided, although technically not required for
// windows created by add-on.
// See: https://developer.mozilla.org/en-US/docs/Security_best_practices_in_extensions#Code_wrapping
if (!httpsEverywhere) { var httpsEverywhere = {}; }
/**
* JS Object for fetching the XML source of rulesets.
*
* @author Pavel Kazakov <nullishzero@gmail.com>
*/
httpsEverywhere.fetchSource = {
// TODO: look into class constants
CC: Components.classes,
CI: Components.interfaces,
// Constants for generating URL from which source will be fetched
BASE_SITE: 'https://gitweb.torproject.org/https-everywhere.git/plain/',
DIRECTORY: '/src/chrome/content/rules/',
HEAD_STRING: 'HEAD',
/**
* Initializes the window to view source.
*/
init: function() {
var fs = httpsEverywhere.fetchSource;
if("arguments" in window && window.arguments.length > 0) {
var filename = window.arguments[0].xmlName;
var id = window.arguments[0].GITCommitID; //GIT commit ID
var URL = fs.getURL(filename, id);
var source = fs.getSource(URL, filename, false);
} else {
// Should never happen
throw 'Invalid window arguments.';
}
},
/**
* Generates a URL that can be used for viewing the ruleset source.
*
* @param filename name of ruleset to view, such as EFF.xml
* @param GITCommitID revision of ruleset
*
* @return string of URL
*/
getURL: function(filename, GITCommitID) {
var fs = httpsEverywhere.fetchSource;
return fs.BASE_SITE + fs.DIRECTORY + filename + "?h=" + GITCommitID;
},
/**
* Sends HTTP request to view ruleset source and updates the window with the
* ruleset source.
*
* @param URL HTTP request will be sent to this URL
* @param filename used for displaying ruleset source
* @param useHead whether send request to latest revision of ruleset
*/
getSource: function(URL, filename, useHead) {
var fs = httpsEverywhere.fetchSource;
fs.setFilenameText(filename);
fs.setPathText(URL);
var req = fs.CC["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(fs.CI.nsIXMLHttpRequest);
// Use HTTP GET
req.open("GET", URL);
// Clear User-Agent so request is pseudo-anonymous
req.setRequestHeader("User-Agent", "");
// handle asynchronous request
req.onreadystatechange = function(params) {
if (req.readyState == 4) {
// HTTP Request was successful
if (req.status == 200) {
fs.setSourceText(req.responseText);
} else if (!useHead) {
// HTTP request was not successful and the request wasn't sent to
// get the latest revision. Therefore, if we can't fetch current
// revision (this project's revision might newer than lastest, for
// example), try to at least display the latest revision.
var URL = fs.getURL(filename, fs.HEAD_STRING);
fs.getSource(URL, filename, true);
} else {
// at least we tried...
fs.downloadFailed();
}
}
};
req.send();
},
/**
* Handle a download failure of ruleset.
*/
downloadFailed: function() {
document.getElementById("source-text").hidden = true;
document.getElementById("failure-label").hidden = false;
},
/**
* Convenience method for setting ruleset source text.
*
* @param text ruleset source
*/
setSourceText: function(text) {
var textBox = document.getElementById("source-text");
textBox.value = text;
},
/**
* Convenience method for setting filename text.
*
* @param text file name
*/
setFilenameText: function (text) {
var textLabel = document.getElementById("filename-text");
textLabel.value = text;
},
/**
* Convenience method for setting the path (URL) that was used to fetch
* ruleset.
*
* @param text path text
*/
setPathText: function(text) {
var textLabel = document.getElementById("path-text");
textLabel.value = text;
}
};
// TODO: Test resizing on mulitple platforms
// adjust window resizing
window.onresize = function() {
var textBox = document.getElementById("source-text");
// TODO: Move to constants
textBox.width = window.innerWidth - 100;
textBox.height = window.innerHeight - 150;
};
// hook event for init
window.addEventListener("load", httpsEverywhere.fetchSource.init, false);
|