Skip to content

Instantly share code, notes, and snippets.

@Infocatcher
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save Infocatcher/9523286 to your computer and use it in GitHub Desktop.

Select an option

Save Infocatcher/9523286 to your computer and use it in GitHub Desktop.
Redirector example for Gecko 20+
// https://gist.github.com/Infocatcher/9523286
// 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/.
// Redirector example for Gecko 20+
// (c) Infocatcher 2014 <https://github.com/Infocatcher>
Services.obs.addObserver(observer, "http-on-modify-request", false);
setTimeout(function() {
Services.obs.removeObserver(observer, "http-on-modify-request");
}, 10e3); // Disable after 10 seconds
function observer(subject, topic, data) {
if(subject instanceof Components.interfaces.nsIHttpChannel) {
var url = subject.URI.spec;
// Example for http://www.yandex.ru/
if(url == "http://yandex.st/jquery/1.8.3/jquery.min.js") {
// Note: we can't redirect to local files (file:// or resource://) due to security restrictions!
//var newUri = Services.io.newURI("data:text/javascript;charset=UTF-8,alert(%22Redirected!%22)%3B", null, null);
var newUri = getDataURI("Z:\\test.js", "text/javascript");
subject.redirectTo(newUri);
}
}
}
function getDataURI(path, type) {
var cache = { __proto__: null };
var expire = 60*1000; // 60 seconds
getDataURI = function(path, type) {
var t = performance.now();
if(path in cache) {
var {uri, timer} = cache[path];
timer.cancel();
}
else {
var data = getFileContents(path);
var dataUrl = "data:" + type + ";charset=UTF-8," + encodeURIComponent(data);
var uri = Services.io.newURI(dataUrl, null, null);
var timer = Components.classes["@mozilla.org/timer;1"]
.createInstance(Components.interfaces.nsITimer);
cache[path] = {
uri: uri,
timer: timer
};
}
timer.init(function() {
delete cache[path];
}, expire, timer.TYPE_ONE_SHOT);
Services.console.logStringMessage("xxx getDataURI(): " + (performance.now() - t).toFixed(3) + " ms");
return uri;
};
return getDataURI.apply(this, arguments);
}
function getFile(path) {
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile || Components.interfaces.nsIFile);
try {
file.initWithPath(path);
}
catch(e) {
Components.utils.reportError(e);
return null;
}
return file;
}
function getFileContents(path) {
var file = getFile(path);
if(!file)
return "";
return readFromFile(file);
}
function readFromFile(file) {
var fis = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
try {
fis.init(file, 0x01, parseInt("0444", 8), 0);
}
catch(e) {
Components.utils.reportError(e);
return "";
}
var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
.createInstance(Components.interfaces.nsIScriptableInputStream);
sis.init(fis);
var str = sis.read(fis.available());
sis.close();
fis.close();
return convertToUnicode(str);
}
function convertToUnicode(str) {
var suc = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
suc.charset = "utf8";
try {
return suc.ConvertToUnicode(str);
}
catch(e) {
Components.utils.reportError(e);
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment