Index: lib/typoCollector.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/typoCollector.js |
@@ -0,0 +1,224 @@ |
+/* |
Wladimir Palant
2012/12/13 16:07:00
This one has been properly renamed to keep history
Thomas Greiner
2013/01/14 10:06:41
Yes, it was IIRC.
|
+ * This file is part of the URL Fixer, |
+ * Copyright (C) 2006-2012 Eyeo GmbH |
+ * |
+ * URL Fixer is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * URL Fixer is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with URL Fixer. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+Cu.import("resource://gre/modules/Services.jsm"); |
+ |
+let {Prefs} = require("prefs"); |
+let {WindowObserver} = require("windowObserver"); |
+ |
+let DOMAIN_TYPED = 1; |
+let DOMAIN_TYPO = 2; |
+let DOMAIN_CORRECTION = 3; |
+let DOMAIN_FALSE_POSITIVE = 4; |
+ |
+let domains = null; |
+let timer = null; |
+ |
+// Initialize and make sure to react to pref changes |
+if (Prefs.domainOptIn) |
+ startCollection(); |
+Prefs.addListener(function(name) |
+{ |
+ if (name != "domainOptIn") |
+ return; |
+ if (Prefs.domainOptIn) |
+ { |
+ Prefs.domainOptInAsk = true; |
+ startCollection(); |
+ } |
+ else |
+ stopCollection(); |
+}); |
+ |
+// Make sure to intercept TypedItCollectorInit event from web pages |
+new WindowObserver({ |
+ applyToWindow: function(window) |
+ { |
+ if (window.document.documentElement.getAttribute("windowtype") != "navigator:browser") |
+ return; |
+ |
+ window.document.addEventListener("TypedItCollectorInit", initWebUI, false, true); |
+ }, |
+ |
+ removeFromWindow: function(window) |
+ { |
+ if (window.document.documentElement.getAttribute("windowtype") != "navigator:browser") |
+ return; |
+ |
+ window.document.removeEventListener("TypedItCollectorInit", initWebUI, false, true); |
+ } |
+}); |
+ |
+exports.onBrowserInitialized = onBrowserInitialized; |
+function onBrowserInitialized(window) |
+{ |
+ // Don't do anything on Fennec. |
+ if ("Browser" in window || "BrowserApp" in window) |
+ return; |
+ |
+ if (Prefs.counter < 5) |
+ Prefs.counter++; |
+ else if (!Prefs.domainOptInAsk && !Prefs.domainOptIn) |
+ window.openDialog("chrome://url-fixer/content/typedItOptIn.xul", "typedItOptIn", "chrome,dialog,centerscreen,titlebar"); |
+} |
+ |
+exports.processTypedDomain = processTypedDomain; |
+function processTypedDomain(domain) |
+{ |
+ if (domains && !privateBrowsingEnabled()) |
+ domains[domain] = DOMAIN_TYPED; |
+} |
+ |
+exports.processDomainCorrection = processDomainCorrection; |
+function processDomainCorrection(domainFrom, domainTo) |
+{ |
+ if (domains && !privateBrowsingEnabled()) |
+ { |
+ domains[domainFrom] = DOMAIN_TYPO; |
+ domains[domainTo] = DOMAIN_CORRECTION; |
+ } |
+} |
+ |
+exports.processFalsePositive = processFalsePositive; |
+function processFalsePositive(domainFrom, domainTo) |
+{ |
+ if (domains && !privateBrowsingEnabled()) |
+ { |
+ domains[domainFrom] = DOMAIN_FALSE_POSITIVE; |
+ domains[domainTo] = DOMAIN_TYPED; |
+ } |
+} |
+ |
+exports.processUserCorrection = processUserCorrection; |
+function processUserCorrection(domainFrom, domainTo) |
+{ |
+ if (domains && !privateBrowsingEnabled()) |
+ { |
+ domains[domainFrom] = DOMAIN_TYPO; |
+ domains[domainTo] = DOMAIN_CORRECTION; |
+ } |
+} |
+ |
+exports.openDisclosurePage = openDisclosurePage; |
+function openDisclosurePage() |
+{ |
+ let window = Services.wm.getMostRecentWindow("navigator:browser"); |
+ if (!window) |
+ return; |
+ |
+ let url = "http://urlfixer.org/data/"; |
+ if ("Browser" in window && typeof window.Browser.addTab != 'undefined') |
+ window.Browser.addTab(url, true); |
+ else if ("gBrowser" in window) |
+ window.gBrowser.loadOneTab(url, {inBackground: false}); |
+} |
+ |
+function startCollection() |
+{ |
+ if (domains) |
+ return; |
+ |
+ onShutdown.add(stopCollection); |
+ |
+ domains = {}; |
+ |
+ // Send data every 60 minutes |
+ timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
+ timer.initWithCallback(sendAnonymousData, 1000 * 60 * 60, Ci.nsITimer.TYPE_REPEATING_SLACK); |
+} |
+ |
+function stopCollection() |
+{ |
+ if (!domains) |
+ return; |
+ |
+ onShutdown.remove(stopCollection); |
+ domains = null; |
+ |
+ try |
+ { |
+ timer.cancel(); |
+ } |
+ catch (e) |
+ { |
+ Cu.reportError(e); |
+ } |
+ timer = null; |
+} |
+ |
+function privateBrowsingEnabled() |
+{ |
+ if (!("service" in privateBrowsingEnabled)) |
+ privateBrowsingEnabled.service = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService); |
+ |
+ return privateBrowsingEnabled.service.privateBrowsingEnabled; |
+} |
+ |
+function sendAnonymousData() |
+{ |
+ if (!Prefs.domainOptIn || privateBrowsingEnabled()) |
+ return; |
+ |
+ let postData = JSON.stringify(domains); |
+ if (postData == JSON.stringify({})) |
+ return; |
+ |
+ let savedDomains = domains; |
+ domains = {}; |
+ |
+ let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest); |
+ request.open("POST", "http://urlfixer.org/submitData"); |
+ request.setRequestHeader("Content-Type", "application/json"); |
+ request.addEventListener("load", function(event) |
+ { |
+ if (event.target.status != 200) |
+ for (var i in savedDomains) |
+ domains[i] = savedDomains[i]; |
+ }, false); |
+ request.send(postData); |
+} |
+ |
+function initWebUI(event) |
+{ |
+ if (Prefs.domainOptIn) |
+ return; |
+ |
+ let container = event.target; |
+ let source = container.ownerDocument.defaultView.location.hostname; |
+ if (!/(^|\.)urlfixer\.org$/.test(source)) |
+ return; |
+ |
+ let button = container.getElementsByClassName("allowButton")[0]; |
+ let message = container.getElementsByClassName("confirmationMessage")[0]; |
+ |
+ if (!button || !message) |
+ return; |
+ |
+ button.addEventListener("click", function(event) |
+ { |
+ if (!event.isTrusted) |
+ return; |
+ |
+ Prefs.domainOptInAsk = true; |
+ Prefs.domainOptIn = true; |
+ button.style.display = "none"; |
+ message.style.display = ""; |
+ }, false); |
+ |
+ message.style.display = "none"; |
+ container.style.display = ""; |
+} |