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);
Related
I'm trying to find a way in javascript to check which URL is loaded, then have a popup notifying the user to update their old bookmarket and have it redirect to the new location in a few seconds.
For example, the url maybe Http:\abc\myappage and I want to check if they are on the http:\abc site which if they are, the notification pops up and redirects them.
Currently I have a simple redirect to take them to the new site, but I never considered anyone that has an old bookmark which would never get updated if you don't inform them about the change.
Thanks.
You can access the current url from within JavaScript with window.location.
Using window.location you can access the current domain and path, then by setting window.location.href = 'your new site' after a few seconds or after some user interaction will cause the browser to navigate to the supplied url.
if(window.location.host === 'abc'){
alert('This url is no longer valid.');
window.location.href = 'http://abc/myappage
}
You can use window.location to get some information regarding the current url:
window.location.origin in the console on this current page, prints:
"http://stackoverflow.com"
Then you could run some JS logic to check against your other url and use alert() to crete the pop up.
working JSBIN: https://jsbin.com/gijola/edit?js,console
adding code:
function checker (url) {
var here = window.location.origin;
l(here);
if (here !== 'whatever you want to check') {
alert('please update your bookmark!!');
}
}
I'm trying to do this by using a Tampermonkey Script. However I'm open to new approaches...
What I want to do is extract some data (data-video), from a specific <div>. However this data is not available under the HTML code of the page, but it's available under Dev Tools -> Resources and then on Frames.
Anyone knows if it's possible to get that information available under DevTools? And how can I do that?
Comparative between the two pages can be found here: "Original HTML PAGE" and "HTML PAGE under DevTools"
On the first hyperlink the id=video-canvas cannot be seen, however it's on the <object type="application/x-shockwave-flash(...)
As you state in your question the data you're looking for is available in DevTools under the "Resources" tab in the "Frames" folder. What you are looking at there is the Source HTML, similar to View Source.
The code you want, is what is getting replaced. It appears the site is using the JW Player Plugin, which is replacing the <div id="video-canvas"> with the appropriate HTML for the device / browser detected to play the video. With all of my browsers on my Mac, they are being forced to use the Flash, even when it's disabled. When using my iPhone, which can't play flash , and inspecting the page it uses JW's own custom video element. It appears that it must be storing the file location in memory since it is not in the generated markup.
I am able to run through the console in the dev tools and access their JS class. It appears i can call jwplayer._tracker , which has an object b . Object b has an object AlWv3iHmEeOzwBIxOUCPzg This object seems to be consistent each time i check between different browsers, you can use the for loop inmy first example to get the correct value but tirmming it down to .b Following that object is e and in e is the object http://i.n.jwpltx.com/v1.... really long string that appears to contain a url, so it will need to parsed.
So to get the HTML string i ran
for ( var loc in jwplayer._tracker.b.AlWv3iHmEeOzwBIxOUCPzg.e){
loc
}
so if we put that in a function to parse the string and return a value
function getSubURL(){
var initURL;
for ( var loc in jwplayer._tracker.b.AlWv3iHmEeOzwBIxOUCPzg.e){
initURL = loc;
}
//look for 'mp4:' this is in front of the file path
var start = initURL.indexOf("mp4%3A");
//look for the .mp4 for the end of the file name
var stop = initURL.indexOf(".mp4");
//grab the string between
//start+6 to remove characters used to find it
//and stop+4 to include characters used to find it
var subPath = (initURL.substring((start+6),(stop+4))).split("%2F").join("/");
return subPath;
}
//and run it
getSubURL();
it will return ciencia/astronomia/fimsol.mp4
you can run this from your console, but I am unaware of how you can use this in Tamper Monkey, but i think it gets ya a lot closer to what you wanted.
This is the approach I've used to solve my problem... I couldn't grab the code I want under Dev Tools, but I find a way to get the data from jwplayer with the function getPlaylistItem. And this is how I get the url filename of each video:
function getFilename(filename) {
var filename;
if(jwplayer().getPlaylistItem){
filename = jwplayer().getPlaylistItem()['file'];
}
else{
return filename;
}
filename = filename.substring(filename.indexOf("/mp4:") + 5);
return filename;
}
I've been spending hours trying to figure out what is wrong with this code, i found out that i cannot reach the updateuser.php file, its on the same directory, the filenames are corret, is there something wrong with this code:
<script>
function updateuseracc(form, password)
{
var p = document.createElement("input");
form.appendChild(p);
p.name="p";
p.type="hidden";
p.value=hex_sha512(password.value);
password.value="";
var useremail=$("#curemail").val();
$.post('updateuser.php',{email:useremail,p:p}).done(function(data){
alert(data);
});
}
</script>
Bring up development tools with F12 on Google Chrome, then go to 'Network' option and check if after calling the script a 500 header page is returned. You can check this at the list of resources called that you will see at development bar.
If so, you will know that you are not referring to the script correctly, also you will get information from the parsed URL so you can guess how can you set the path to updateuser.php.
i was searching google to get any js lib which can capture the image of any website or url. i came to know that phantomjs library can do it. here i got a small code which capture and convert the github home page to png image
if anyone familiar with phantomjs then please tell me what is the meaning of this line
var page = require('webpage').create();
here i can give any name instead of webpage ?
if i need to capture the portion of any webpage then how can i do it with the help of this library. anyone can guide me.
var page = require('webpage').create();
page.open('http://github.com/', function () {
page.render('github.png');
phantom.exit();
});
https://github.com/ariya/phantomjs/wiki
thanks
Here is a simple phantomjs script for grabbing an image:
var page = require('webpage').create(),
system = require('system'),
address, output, size;
address = "http://google.com";
output = "your_image.png";
page.viewportSize = { width: 900, height: 600 };
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
window.setTimeout(function () {
page.render(output);
console.log('done');
phantom.exit();
}, 10000);
}
})
Where..
'address' is your url string.
'output' is your filename string.
Also 'width' & 'height' are the dimensions of what area of the site to capture (comment this out if you want the whole page)
To run this from the command line save the above as 'script_name.js and fire off phantom making the js file the first argument.
Hope this helps :)
The line you ask about:
var page = require('webpage').create();
As far as I can tell, that line does 3 things: It adds a module require('webpage'), then creates a WebPage Object in PhantomJS .create(), and then assigns that Object to var = page
The name "webpage" tells it which module to add.
http://phantomjs.org/api/webpage/
I too need a way to use page.render() to capture just one section of a web page, but I don't see an easy way to do this. It would be nice to select a page element by ID and just render out that element based at whatever size it is. They should really add that for the next version of PhantomJS.
For now, my only workaround is to add an anchor tag to my URL http://example.com/page.html#element to make the page scroll to the element that I want, and then set a width and height that gets close to the size I need.
I recently discovered that I can manipulate the page somewhat before rendering, so I want to try to use this technique to hide all of the other elements except the one I want to capture. I have not tried this yet, but maybe I will have some success.
See this page and look at how they use querySelector(): https://github.com/ariya/phantomjs/blob/master/examples/technews.js
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;
}