javascript - Firefox Extension location.href not redirecting -
i'm using rather generic here, want able load new tab @ desired url when selecting extension, when @ tab, redirect new url. (the add on should run code @ first page before redirecting, that's day).
the code have @ moment is
var buttons = require('sdk/ui/button/action'); var tabs = require("sdk/tabs"); var button = buttons.actionbutton({ id: "redirect", label: "redirect", icon: { "16": "./icon-16.png" }, onclick: handleclick }); function handleclick(state) { tabs.open({ url: "http://www.google.com", onready: loadredirect }); function loadredirect(tab) { tab.attach({ contentscript: "location.href = 'www.youtube.com;'" }); } }
when running however, 2nd url appends first, rather replaces, , gets stuck in infinite load/refresh loop until close browser.
i assume i'm missing absolutely obvious, wasn't able find while searching around.
you trying change location.href
string has no scheme. assumed url within current domain. because not start /
assumed relative current page. thus, appended current google.com
url. page becomes ready; fires ready
event; , onready
handler called. handler changes url, causing page reloaded, and, again, fire ready
event, starts process on again. infinite loop.
to www.youtube.com
, change code to:
function loadredirect(tab) { tab.attach({ contentscript: "location.href = 'https://www.youtube.com';" }); }
which have been done without need inject content script assigning tab.url
:
function loadredirect(tab) { tab.url = 'https://www.youtube.com'; }
however, not prevent infinite loop. there many ways so. easiest remove ready
listener:
function loadredirect(tab) { tab.off('ready',loadredirect); tab.url = 'https://www.youtube.com'; }
Comments
Post a Comment