Browser Extension - How to get the current page's URL - javascript

I am making a custom button. When I click that,it should show the "current page's URL".
I found the answer as "document.location" or "windows.location" . But, both points to the local XUL location "chrome://browser/content/browser.xul" not the original URL. Can anybody show how to accomplish this?

Try anyone of these.. One should definitely work,
window.top.getBrowser().selectedBrowser.contentWindow.location.href;
window.content.location.href
function getURL{
var currentWindow = Components.classes["#mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
var currBrowseSession = currentWindow.getBrowser();
var currURL = currBrowseSession.currentURI.spec;
return currURL;
}

Related

Change current URL then load

I want jquery to find and change the current URL you are visiting and load the new one. Lets say jquery is supposed to change the current "index.html" to "indexalt.html" on click and then load it. My idea is to use document.URL to get the current URL then slice off ".html" at the end and add for example "alt.html" to string. I'm very new to jquery and don't get it to work. There will be a lot of mistakes within my script probably:
$("#contact").on('click', function() {
var url=document.URL(str.slice(-7));
.load("url +"alt.html"");
I would really appreciate if anyone can tell me how to do it and how to write it down correctly in a whole. Thanks!
location.href returns the URL of the page. So, you can do this instead.
$("#contact").on("click", function() {
// Slicing 5 characters from the URL.
const url = location.href.slice(0, -5)
// Simply load the URL
location.href = url + "alt.html"
})

Javascript Redirect sends visitors to Wrong URL

I have a redirect that happens after a user submits a form. Users should move through the funnel like this:
SiteA.com/submit-form/ ---> SiteB.com/payment/
Problem - The redirect ignores the domain name (i.e. SiteB). It sends visitors to SiteA.com/payment/
Here's the code:
jQuery(document).ready(function(){
var tracker = ga.getAll()[0];
var linkerParam = tracker.get('linkerParam');
var url = 'https://siteB.com/payment/?' + linkerParam;
window.setTimeout(window.location = url, 4000);
});
This code is meant to append Google Analytics cross-domain tracking code. The form plugin (Formidable Pro) also has a redirect option built-in. When activated, it also redirects to the wrong page.
I'm not sure where to start looking. I had added a Category Base in Permalinks. I've just removed it, hoping this might be the problem. Any ideas / help would be greatly appreciated!
window.location return object and in strict mode you will get exception in Chrome.
Full explanation you can find here
You just need
window.setTimeout(function(){window.location.href = url}, 4000);

C# WebBrowser control window.name

I am trying to automate a site in a WPF application with WebBrowser control.
The site checks for the javascript window.name in each page and throws an error if this does not match with the preset value.
Look at the sample below.
var id="1234";
if (window.name != id)
{
window.open("home.html", id)
}
Is there a way to get this value and set it when I create a new WebBrowser object?
I tried the following and my problem is resolved. Hope this may help somebody.
I first navigated the page to a blank page with this code.
var html = string.Format(
"<html><body><h4>Opening ...</h4><script type='text/javascript'>window.open('about:blank', '{0}');</script></body></html>",
popupWindowName);
var w = new Browser();
w.NavigateToString(html);
And then in the page is load completed event, I navigated to the original URL.
w.Navigate("https://somesite.com/page.aspx",
null, null, h);
The popup window name was changed to what I wanted and the session continuted correctly. This is not a solution to the problem I faced, but it is more like a work around.
I also had to deal with the popups that kept coming. I had handled the NewWindow2 event to handle the popups.

Getting Document type icon for a document in Sharepoint

I am trying to get the icon url/name corresponding to document retrieved from a SharePoint document library using the following javascript code (i am using JSOM):
function GetIcon(filename)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var iconName;
iconName = web.mapToIcon(filename, '', SP.Utilities.IconSize.Size16);
var iconUrl = "/_layouts/images/" + iconName.get_value();
alert(iconUrl);
}
i cant observe any problem in the code but it always shows icon name as '0' than displaying the real icon name (i.e. icdoc.gif, ictxt.gif etc).
Am i missing something here?
Please guide me through this.
Your code works fine for me. It even works if the file does not exist and with an unrecognized file extension. Also, permissions do not appear to be involved.
If you browse to the page using Chrome and look on the Network tab of the Developer Tools (F12), you can view the raw response of the request. The name of the request is "Process Query". The image below shows the area I am referring to. This should give you some more insight on the problem.
iconName will be only populated after calling executeQueryAsync
context.executeQueryAsync(function() {
var iconUrl = "/_layouts/images/" + iconName.get_value();
alert(iconUrl);
}, function() { alert("Errors"); });

Firefox addon / javascript: get url from bar

I followed the example code from the addon dev site have successfully put a button onto FF :)
now i want to make that button do something interesting so I thought I would run an alert with the address that is currently in the bar... but this does not work:
CustomButton = {
1: function () {
alert("Just testing 1"+document.location.href);
},
}
except for the +document.location.href it's the exact demo code I got from the dev site...
You should note that in extension developing, document and window variables refers to the Host Browser not the browser that contains the web site.
you should use
gBrowser.selectedTab
for getting current tab and then using
currentURI.host
for getting URL host
also note that selectedTab returns a tab variable then you should get the window of that tab.
then the whole code will be:
gBrowser.getBrowserForTab(gBrowser.selectedTab).currentURI.host
Do you want to get the location of current document or the string that is in the location bar?
For location of current document
content.location.href
For the string in location bar
document.getElementById("urlbar").value
or
gURLBar.value
Those work for me, what context are you using it/how are you calling the function?
> document.location.href
< "http://stackoverflow.com/questions/6352035/firefox-addon-javascript-get-url-from-bar"
You can also use window.location.href
> window.location.href
< "http://stackoverflow.com/questions/6352035/firefox-addon-javascript-get-url-from-bar"
Try alert("The current page URL is " + browser.currentURI.spec) and see how that goes for you.
See also:
Firefox extension development : Get URL of new tab and https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIURI
Took a while to figure it all out:
gBrowser.currentURI.spec;
is how you do it.
Here's some code you may find useful
window.addEventListener('load', function (e) {
var href = gBrowser.currentURI.spec;
if ( href.match(/website.com/) ) {
var contentDoc = content.document;
// Do some stuff to the current website.com's DOM
}
}, true);

Categories