Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intika/503ab65f457dc20bb77b to your computer and use it in GitHub Desktop.
Save intika/503ab65f457dc20bb77b to your computer and use it in GitHub Desktop.
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