...

View Full Version : Why wont this bookmarklet work?



rig99
05-15-2009, 08:42 AM
I'm trying to make a bookmarklet for easy use of a website that grabs a video's real location so I can watch it in VLC media player. I am not a javascript programmer but I've looked some stuff up and this should work shouldn't it? So far I have this:



var t, vidurl, mathcode;

start();

function start() {
vidurl=window.location;
window.location="http://clipnabber.com";
setTimeout("end()",100);}

function end() {
alert("in end");
mathcode=document.getElementById("Math").innerHTML;
alert(mathcode);
vidurl=vidurl.replace(/\&/g,"%26");
vidurl=vidurl.replace(/#/g,"%23");
vidurl=vidurl.replace(/=/g,"%3D");
vidurl=vidurl.replace(/\?/g,"%3F");
vidurl=vidurl.replace(/.html/g,"dothtml");
window.location="http://clipnabber.com/gethint.php?mode=1&url="+vidurl+"&sid="+mathcode;}


I added the setTimeout because I don't know how to tell if the page is loaded fully. If there is a better way I'm all ears.

Here is bookmarklet form:



javascript:var t, vidurl, mathcode; start(); function start(){vidurl=window.location; window.location="http://clipnabber.com"; setTimeout("end()",100);} function end(){alert("in end"); mathcode=document.getElementById("Math").innerHTML; alert(mathcode); vidurl=vidurl.replace(/\&/g,"%26"); vidurl=vidurl.replace(/#/g,"%23"); vidurl=vidurl.replace(/=/g,"%3D"); vidurl=vidurl.replace(/\?/g,"%3F"); vidurl=vidurl.replace(/.html/g,"dothtml"); window.location="http://clipnabber.com/gethint.php?mode=1&url="+vidurl+"&sid="+mathcode;}

When I test it the alert box saying "in end" will show but then it stops. I can't get the alert box showing the mathcode to show and it won't go to the new location either. In the firefox error console it says "Error: uncaught exception: unknown (can't convert to string)".

I tried changing alert(mathcode) to alert(String(mathcode)) hoping that would do it but had no success. Please help, I'm not sure what I'm doing wrong here.

Philip M
05-15-2009, 10:19 AM
mathcode is not your problem. The problem is that vidurl is an object, not a string.


function end() {
alert("in end");
mathcode=document.getElementById("Math").innerHTML;
alert(mathcode);
alert (typeof vidurl);
vidurl = vidurl.toString();
alert (typoeof vidurl);
vidurl=vidurl.replace(/\&/g,"%26");


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

rig99
05-16-2009, 03:51 PM
Thank you, I changed vidurl=window.location to vidurl=location.href now I don't get that error about converting string.

I have found another problem though.

There appears to be a 100ms limit for setTimeout. This limit doesn't give me enough time to load all the page so when I try to getElementById('Math') it is null.

example

works -> javascript:location.href="http://clipnabber.com";setTimeout("alert('HI')",10);
works -> javascript:location.href="http://clipnabber.com";setTimeout("alert('HI')",100);
don't see alert box -> javascript:location.href="http://clipnabber.com";setTimeout("alert('HI')",1000);

Is there a possible workaround for this?

Philip M
05-16-2009, 06:16 PM
[QUOTE=rig99;817570]There appears to be a 100ms limit for setTimeout. [QUOTE]

There is no limit to the number of milliseconds in setTimeout().
But when you change the location of the page the content of the previous page is lost. It looks as though it takes 100ms or so for the change to take place.

imrankhan1980ik
05-16-2009, 06:22 PM
Very good information

rig99
05-18-2009, 03:42 PM
So then is what I'm wanting to do even possible?

My goal is to be able to click the bookmarklet when I'm on a site that has the video embedded in it, then get the "Math" code from clipnabber's homepage, and finally have the video site's url and the "Math" code sent to clipnabber's script.

So far none of my methods have worked. I've tried pausing script execution with setTimeout to delay my code so the clipnabber homepage has time to load fully. That doesn't work because, like Phillip says, the script is gone once the clipnabber homepage loads. I've also tried loading the clipnabber homepage into an iframe on the video's page. The problem with that is I can't get any info from the iframed page so I can't get the "Math" code I need.

I find it very hard to believe something like this is impossible because it seems so simple to do. Store current URL, load new page, store a variable from new page, create URL with stored URL and stored variable as arguments. Honestly I thought this would take me no time at all, but here I am on day 5 and still have no solution.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum