Use a javascript variable as search query

in Script development
Subscribe to Use a javascript variable as search query 4 posts, 3 voices



evox777 Scriptwright

I have a javascript variable named akTargetDesc on a webpage which i would like to use as a query word with google.

Here is a link to my script if you wanna take a look:
http://userscripts.org/scripts/show/7939

Here is a snippet from the script
---------------------------------------------------------------------
var newLinkies = "<font><
a
href='http://images.google.com/images?ie=UTF-8&q=akTargetDesc'>Images
<
/a></font>";

var linky = document.createElement('div');
linky.innerHTML = newLinkies;

var targetElement = document.getElementById('new_left'); //removes navigation links from LHS
if (targetElement)
targetElement.parentNode.insertBefore(linky, targetElement);
---------------------------------------------------------------------

 
Brad Stewart User

The way you've got it there, instead of the value of akTargetDesc being put into your link, it'll search for akTargetDesc. Because it's within the string, marked by quotes, the Javascript parser won't know that it's supposed to be a variable.
Also, you close your font tag before the "size attribute".
Try this instead:


var newLinkies = "<font>Images</font>";

 
evox777 Scriptwright

I know. I just put the link like that so you guys would know where i wanted the variable value to be.
i have tried using akTargetDesc in my GreaseMonkey script but since its a variable in the webpage and not in the script, i get a variable undefined error.

I found a workaround by copying Metadata from the webpage to my script as I was unable to copy javascript variables.

Thanks for the quick response buddy...

 
Arvid Scriptwright

If you want a variable declared in the content of the page, you should use unsafeWindow.variable, in your case unsafeWindow.akTargetDesc. However, as the name suggests, there are security issues with doing this. You can just pick it out of the meta-tag instead.