Forked from Noitidart/_ff-addon-snippet-LoadContextAndGoodies.js
Last active
March 21, 2019 22:31
-
-
Save intika/503ab65f457dc20bb77b to your computer and use it in GitHub Desktop.
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
function loadContextAndGoodies(request, return_goodies) { | |
var loadContext = null; | |
if (request instanceof Ci.nsIRequest) { | |
try { | |
if (request.loadGroup && request.loadGroup.notificationCallbacks) { | |
loadContext = request.loadGroup.notificationCallbacks.getInterface(Ci.nsILoadContext); | |
} | |
} catch (ex) { | |
console.exception('request loadGroup with notificationCallbacks but oculd not get nsIloadContext', ex); | |
} | |
if (!loadContext) { | |
try { | |
if (request.notificationCallbacks) { | |
loadContext = request.notificationCallbacks.getInterface(Ci.nsILoadContext); | |
} | |
} catch (ex) { | |
console.exception('request has notificationCallbacks but could not get nsILoadContext', ex); | |
/* start - noit's backup try, it might be redundant (im not sure) as Wladamir Palant didn't have this way*/ | |
try { | |
var interfaceRequestor = httpChannel.notificationCallbacks.QueryInterface(Ci.nsIInterfaceRequestor); | |
loadContext = interfaceRequestor.getInterface(Ci.nsILoadContext); | |
} catch (ex) { | |
console.exception('backup method failed:' ex); | |
} | |
/* end - my backup try, it might be redundant as Wladamir Palant didn't have this way*/ | |
} | |
} | |
} else { | |
console.warn('request argument is not instance of nsIRequest') | |
} | |
if (return_goodies) { | |
if (!loadContext) { | |
return null; | |
} | |
var contentWindow = loadContext.associatedWindow; | |
var DOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor) | |
.getInterface(Ci.nsIWebNavigation) | |
.QueryInterface(Ci.nsIDocShellTreeItem) | |
.rootTreeItem | |
.QueryInterface(Ci.nsIInterfaceRequestor) | |
.getInterface(Ci.nsIDOMWindow); | |
var gBrowser = DOMWindow.gBrowser; | |
if (gBrowser._getTabForContentWindow(contentWindow.top)) { | |
var tab = gBrowser._getTabForContentWindow(contentWindow.top); | |
var browser = tab.linkedBrowser; | |
} else { | |
var tab, browser = null; | |
} | |
var goodies = { | |
loadContext: loadContext, | |
DOMWindow: DOMWindow, | |
gBrowser: gBrowser, | |
contentWindow: contentWindow, | |
browser: browser, | |
tab: tab | |
}; | |
return goodies; | |
} else { | |
return loadContext; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment