From 663d07d9a078e8f8cdb23c5a3d314e8358b90cb5 Mon Sep 17 00:00:00 2001 From: JustOff Date: Sun, 13 Jan 2019 14:43:36 +0200 Subject: [PATCH] Suggest to use CAA on AMO 404-pages --- bootstrap.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/bootstrap.js b/bootstrap.js index 93e552b..79ed82b 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -11,6 +11,7 @@ const CAA_URI = Services.io.newURI(CAA_URL, null, null); const nsIURI = CC("@mozilla.org/network/simple-uri;1", "nsIURI"); const ff47plus = (Services.vc.compare(Services.appinfo.version, 47) > 0); +const hint = '

Try to find "%ADDON%" in the Classic Add-ons Archive.

'; const CAA_MODULES = [ "chrome://ca-archive/content/about.js", @@ -336,6 +337,14 @@ let httpObserver = { storageHost = redirect[1]; } } + } else if (subject.URI.host == "addons.mozilla.org" && !Services.appinfo.browserTabsRemoteAutostart) { + let addon; + if (subject.responseStatus == "404" && (addon = /\/firefox\/addon\/(.*?)\//.exec(subject.URI.path)) !== null) { + subject.QueryInterface(Ci.nsITraceableChannel); + let newListener = new TracingListener(); + newListener.addon = addon[1]; + newListener.originalListener = subject.setNewListener(newListener); + } } } }, @@ -358,6 +367,60 @@ let httpObserver = { } } +function CCIN(cName, ifaceName) { + return Cc[cName].createInstance(Ci[ifaceName]); +} + +function TracingListener() { + this.receivedData = []; +} + +TracingListener.prototype = { + onDataAvailable: function(request, context, inputStream, offset, count) { + let binaryInputStream = CCIN("@mozilla.org/binaryinputstream;1","nsIBinaryInputStream"); + binaryInputStream.setInputStream(inputStream); + let data = binaryInputStream.readBytes(count); + this.receivedData.push(data); + }, + onStartRequest: function(request, context) { + try { + this.originalListener.onStartRequest(request, context); + } catch (err) { + request.cancel(err.result); + } + }, + onStopRequest: function(request, context, statusCode) { + let data = this.receivedData.join(""); + try { + data = data.replace(/
/, "$&" + hint.replace(/%ADDON%/g, this.addon)); + data = data.replace(/ 0) { + os.write(data, data.length); + } + os.close(); + + try { + this.originalListener.onDataAvailable(request, context, storageStream.newInputStream(0), 0, data.length); + } catch (e) {} + + try { + this.originalListener.onStopRequest(request, context, statusCode); + } catch (e) {} + }, + QueryInterface: function(aIID) { + if (aIID.equals(Ci.nsIStreamListener) || aIID.equals(Ci.nsISupports)) { + return this; + } else { + throw Cr.NS_NOINTERFACE; + } + } +} + function startup(data, reason) { if (!styleSheetService.sheetRegistered(styleSheetURI, styleSheetService.USER_SHEET)) { styleSheetService.loadAndRegisterSheet(styleSheetURI, styleSheetService.USER_SHEET);