Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vroy
Created September 1, 2011 01:56
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 vroy/1185231 to your computer and use it in GitHub Desktop.
Save vroy/1185231 to your computer and use it in GitHub Desktop.
Jetpack - Change window location from content script.
console.log("Starting content script.");
console.log( window.location );
window.location = "http://www.google.com/";
console.log( window.location );
console.log( window.location.host );
var data = require('self').data;
require('page-mod').PageMod({
include: "*",
contentScriptWhen: 'ready',
contentScriptFile: [ data.url('example.js') ]
});
console.log("Addon loaded.");
info: Addon loaded.
info: Starting content script.
info: http://www.facebook.com/
info: http://www.google.com/
info: undefined
Using the addon-sdk from github.com/mozilla/addon-sdk with this patch applied: https://bugzilla.mozilla.org/attachment.cgi?id=555753&action=edit
As you can see in the output, the location does seem to change but when I hit facebook.com and the output shows that I should be on google.com, I still see facebook.com. Is it possible to change the location of a page from within the page-mod's content script?
I would also like to point out that window.location.host returns undefined. I thought it would make sense that window.location in the context of a content script should return a standard Location object as described by the MDN docs: https://developer.mozilla.org/En/Window.location#Location_object
I'm guessing the workaround is to send messages to the add-on code and use the tabs API but I would rather avoid that to keep my extension code as much similar to other browser extensions as possible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment