Last active
August 29, 2015 14:04
-
-
Save Infocatcher/a32145ee13dd77d43513 to your computer and use it in GitHub Desktop.
Auto Private, https://addons.mozilla.org/addon/auto-private/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Auto Private, https://addons.mozilla.org/addon/auto-private/ (code from v.1.5 + some modifications) | |
var prefs = { | |
name: ["extensions.autoprivate.domains", "extensions.autoprivate.parts"], | |
domains: "", | |
parts: "", | |
init: function() { | |
var name1 = this.name[0]; | |
var name2 = this.name[1]; | |
var defaultBranch = Services.prefs.getDefaultBranch(""); | |
if(defaultBranch.getPrefType(name1) != defaultBranch.PREF_STRING) | |
defaultBranch.deleteBranch(name1); | |
if(defaultBranch.getPrefType(name2) != defaultBranch.PREF_STRING) | |
defaultBranch.deleteBranch(name2); | |
defaultBranch.setCharPref(name1, ""); | |
defaultBranch.setCharPref(name2, ""); | |
this.readBlacklists(); | |
Services.prefs.addObserver(name1, this, false); | |
Services.prefs.addObserver(name2, this, false); | |
}, | |
destroy: function() { | |
Services.prefs.removeObserver(this.name[0], this); | |
Services.prefs.removeObserver(this.name[1], this); | |
}, | |
observe: function(subject, topic, pName) { | |
this.readBlacklists(); | |
}, | |
readBlacklists: function() { | |
var domains = Services.prefs.getComplexValue(this.name[0], Ci.nsISupportsString).data; | |
if(/\//.test(domains)) | |
Services.prefs.setCharPref(this.name[0], domains.replace(/(\w*:\/{2,4})?([^/;]+)\/?.*?(;|$)/g, "$2$3")); | |
this.domains = Services.prefs.getComplexValue(this.name[0], Ci.nsISupportsString).data; | |
this.parts = Services.prefs.getComplexValue(this.name[1], Ci.nsISupportsString).data; | |
} | |
}; | |
var httpRequestObserver = | |
{ | |
observe: function(subject, topic, data) | |
{ | |
if(subject instanceof Ci.nsIHttpChannel && subject.URI){ | |
var eTldService = Components | |
.classes["@mozilla.org/network/effective-tld-service;1"] | |
.getService(Ci.nsIEffectiveTLDService); | |
var tld = eTldService.getPublicSuffix(subject.URI); | |
var base = eTldService.getBaseDomain(subject.URI); | |
var prefix = subject.URI.host.replace(base, ""); | |
var domains = prefs.domains.replace(/(.+?)\.tld(;|$)/g, "$1." + tld + "$2").replace(/(^|;)\./g, "$1" + prefix); | |
var parts = new RegExp("^(" + prefs.parts.replace(/(.+?)\.tld(\/.*?|\*?)(;|$)/g, "$1." + tld + "$2$3").replace(/([\w*]*:\/{2,4})\./g, "$1" + prefix).replace(/([?^$+{}[\]/.\\])/g, "\\$1").replace(/\*/g, ".*").replace(/^;|;$/g, "").replace(/;/g, "|") + ")$", "i"); | |
if( | |
domains.split(";").indexOf(subject.URI.host) != -1 | |
|| parts.test(subject.URI.spec) | |
) { | |
var httpChannel = subject; | |
var goodies = loadContextGoodies(httpChannel); | |
if(goodies) { | |
var loadContext = goodies.loadContext; | |
if(loadContext.usePrivateBrowsing) // Already private | |
return; | |
var window = goodies.aDOMWindow; | |
var tab = goodies.aTab; | |
loadContext.usePrivateBrowsing = true; | |
if("privateTab" in window) { | |
tab.dispatchEvent(new window.UIEvent("PrivateTab:PrivateChanged", { | |
bubbles: true, | |
cancelable: false, | |
detail: 1, | |
view: window | |
})); | |
} | |
else { | |
tab.setAttribute("autoPrivate-isPrivate", "true"); | |
var s = tab.style; | |
s.textDecoration = "underline"; | |
s.textDecorationColor = s.MozTextDecorationColor = "red"; | |
s.textDecorationStyle = s.MozTextDecorationStyle = "dashed"; | |
} | |
httpChannel.redirectTo(subject.URI.clone()); | |
} | |
} | |
} | |
}, | |
register: function() | |
{ | |
Services.obs.addObserver(this, "http-on-modify-request", false); | |
}, | |
unregister: function() | |
{ | |
Services.obs.removeObserver(this, "http-on-modify-request"); | |
} | |
}; | |
function loadContextGoodies(httpChannel) { | |
var loadContext; | |
try { | |
var interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor); | |
try { | |
loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext); | |
} catch (ex) { | |
try { | |
loadContext = subject.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext); | |
} catch (ex2) {} | |
} | |
} catch (ex0) {} | |
if (!loadContext) { | |
return null; | |
} else { | |
var contentWindow = loadContext.associatedWindow; | |
if (!contentWindow) { | |
return null; | |
} else { | |
//Services.console.logStringMessage("[Auto Private] " + contentWindow.location.href + "\n" + httpChannel.URI.spec); | |
if ( | |
contentWindow != contentWindow.top // Ignore frames | |
|| contentWindow instanceof Ci.nsIDOMChromeWindow // Doesn't looks like content window | |
|| !(httpChannel instanceof Ci.nsIRequest) // Grant access to nsIRequest.loadFlags | |
|| !(httpChannel.loadFlags & httpChannel.LOAD_DOCUMENT_URI) | |
) | |
return null; | |
var aDOMWindow = contentWindow.QueryInterface(Ci.nsIInterfaceRequestor) | |
.getInterface(Ci.nsIWebNavigation) | |
.QueryInterface(Ci.nsIDocShellTreeItem) | |
.rootTreeItem | |
.QueryInterface(Ci.nsIInterfaceRequestor) | |
.getInterface(Ci.nsIDOMWindow); | |
var gBrowser = aDOMWindow.gBrowser; | |
if (!gBrowser) | |
return null; | |
var aTab = gBrowser._getTabForContentWindow(contentWindow); | |
if (!aTab) | |
return null; | |
return { | |
loadContext: loadContext, | |
aDOMWindow: aDOMWindow, | |
gBrowser: gBrowser, | |
aTab: aTab, | |
contentWindow: contentWindow | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment