I wanted to know how to modify the URL of a page when it loads, using a Firefox plugin. I was directed to advice such as the following. Specifically, when a user goes to "https://www.google.com" through the address bar, I wanted to redirect to "https://www.google.co.uk".
For my specific purposes, the above solution almost works, but it redirects too many URLs. One problem that I have found with this is as follows: While I can redirect every time, I sometimes redirect when I don't want to. For instance, it seems that in Windows, I not only redirect when the current tab's URL is regular Google, but I also redirect whenever some query that a page makes contains the above link (e.g. a news article).
Is there any way to use Mozilla's observer framework to redirect by checking the current tab's URL ONLY? If not, is there some alternate solution that still uses Mozilla's Add-on SDK?
Edit: Below is my buggy source code.
var myObserver = {
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, TOPIC_MODIFY_REQUEST, false);
},
//observe function to capture the changed event
observe : function(aSubject, aTopic, aData) {
if (TOPIC_MODIFY_REQUEST == aTopic ) {
var url;
aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
url = aSubject.URI.spec;
url = encodeURIComponent(url);
if (url.indexOf("google.nl") !=-1 || url.indexOf("hl%3Dnl") !=-1
|| url.indexOf("google.com%2Fnl") !=-1 || url == "https%3A%2F%2Fwww.google.com"
|| url == "https%3A%2F%2Fwww.google.com%2Fncr" || url ==
"https%3A%2F%2Fwww.google.com%2F%3Fgws_rd%3Dssl")
{
aSubject.cancel(Components.results.NS_BINDING_ABORTED);
loadURL("https://www.google.co.uk/");
}
}
}
function loadURL(url) {
// Set the browser window's location to the incoming URL
window._content.document.location = url;
// Make sure that we get the focus
window.content.focus();
}