Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[SOLVED] Javascript w/ AHK via Bookmarklets


  • Please log in to reply
5 replies to this topic
Logiq121
  • Members
  • 17 posts
  • Last active: Mar 25 2009 12:09 AM
  • Joined: 03 Feb 2009
Hello guys. I am new to the forum and I have a question about creating a bookmarklet for my AHK automation. If this is the wrong location to post this, please let me know where I can.

I was able to make a selection inside of Form Select Box by following this model:

javascript:document.formname.fieldname.focus()

With the results being this:

javascript:document.aspnetForm.ctl00$Container$SubsGrid$ctl02$SubStatusList.selectedIndex = 2; void(0);

I am looking for some help with accessing a HTML link using a bookmarklet like the one above. If I can get it to focus and select the link it would be golden, but I at least need it to focus on the link so I can just hit the "spacebar." Here is the source of the href:

<a href="javascript:__doPostBack('ctl00$Container$SubsGrid','Edit$1')">Edit</a>

Although it is not shown, is it part of the Form named "aspnetForm"

I appreciate any help.

n-l-i-d
  • Guests
  • Last active:
  • Joined: --

javascript:__doPostBack('ctl00$Container$SubsGrid','Edit$1')


HTH

Logiq121
  • Members
  • 17 posts
  • Last active: Mar 25 2009 12:09 AM
  • Joined: 03 Feb 2009
I really didn't think it would be that easy. Thank you very much.

Now what if the javascript wasn't embeded into the href? For example:

<A href="www.myweb.com/link">My Web</A>

I know it would that you could just type that into the address bar. But If I absolutely needed it to be clicked, then how?

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
From earlier posting here.

Save the following code as html file and open it.

Try the links to set values, and the submit button to get the values. Use the reset button to clear. View source for details.

<html> 
<head> 
<title>HTML Template</title> 

<script type="text/javascript"> 

function getValues() { 
  data2Show = "formName: " + document.forms[0].name + 
              "\rformID: " + document.formName.id + 
              "\rformAction: " + document.formID.action + 
              "\rformMethod: " + document.formName.method + 
              "\rtextID: " + document.formName.textID.id + 
              "\rtextValue: " + document.formName.textID.value + 
              "\rhiddenName: " + document.formName.elements[1].name + 
              "\rhiddenValue: " + document.forms[0].hiddenName.value + 
              "\rcheckboxName: " + document.formName.elements[2].name + 
              "\rcheckboxValue: " + document.formName.checkboxName.value + 
              "\rcheckboxChecked: " + document.formName.checkboxName.checked + 
              "\rradioName: " + document.formName.elements[3].name + 
              "\rradio1Value: " + document.formName.radioName[0].value + 
              "\rradio2Value: " + document.formName.radioName[1].value + 
              "\rradio1Checked: " + document.formName.radioName[0].checked + 
              "\rradio2Checked: " + document.formName.radioName[1].checked + 
              "" 
  alert(data2Show) ; 
} 

function setValues(x) { 
  switch (x){ 
    case 1: 
      document.formName.textID.value = "new text"; 
      break 
    case 2: 
      document.formName.hiddenName.value = "0987654321"; 
      break 
    case 3: 
      document.forms[0].elements[2].checked = "true"; 
      break 
    case 4: 
      document.formName.radioName[1].checked = "true"; 
      break 
    case 5: 
      document.formName.textID.focus(); 
      break 
    case 6: 
      document.formName.submit(); 
      break 
  } 
} 

</script> 

</head> 
<body> 
Try the links to set values, and the submit button to get the values. Use the reset button to clear. View source for details. 
 
<FORM name="formName" id="formID" action="javascript:getValues();" method="post"> 
<INPUT type="text" id="textID"> A textbox 
<input type="hidden" name="hiddenName" value="1234567890">  
<input type="checkbox" name="checkboxName" value="0"> A checkbox 
<input type="radio" name="radioName" value="0" checked="checked">A radiobutton  
<input type="radio" name="radioName" value="1">Another radiobutton   
<INPUT type="submit" value="Show values">   <INPUT type="reset"> 
</form> 
   
<a href="javascript:setValues(1);">Change the text field</a>  
<a href="javascript:setValues(2);">Change hiddenName value</a>  
<a href="javascript:setValues(3);">Check checkbox</a>  
<a href="javascript:setValues(4);">Check radiobutton #2</a>  
<a href="javascript:setValues(5);">Focus the text field</a>  
<a href="javascript:setValues(6);">Submit the form</a>  

</body> 
</html>

Posted Image

For controlling links, check out the javascript document.links object

document.links[i].innerText
document.links[i].href
document.links[i].click()
document.links[i].focus()

; by index/number
javascript:document.links[[color=red]215[/color]].click();void(0);

; by text of link, focus
javascript:z='[color=red]Text of link[/color]';for(x=0;x<document.links.length;x++){y=document.links[x];if(y.[color=red]innerText[/color].indexOf(z)!=-1){y.[color=red]focus()[/color];};}void(0);

; by url of link, click
javascript:z='[color=red]URL of link[/color]';for(x=0;x<document.links.length;x++){y=document.links[x];if(y.[color=red]href[/color].indexOf(z)!=-1){y.[color=red]click()[/color];};}void(0);


W3 Schools - JavaScript Tutorial, JavaScript/HTML DOM Reference, HTML DOM Examples

HTH

Logiq121
  • Members
  • 17 posts
  • Last active: Mar 25 2009 12:09 AM
  • Joined: 03 Feb 2009
Thanks for your help guys. I finally took the initiative to understand the syntax

javascript:z='Text of link';for(x=0;x<document.links.length;x++){y=document.links[x];if(y.innerText.indexOf(z)!=-1){y.focus();};}void(0);

Let me know if I am wrong. So the code above searches through all the inner text (the text between <a> and the </a> example: <a href"www.myweb.com">Text goes here!</a> )links on the page until it finds one that matches z.

With that said, if all that is true then this should work

javascript:z='9101805213907922769753';for(x=0;x<document.links.length;x++){y=document.links[x];if(y.innerText.indexOf(z)!=-1){y.focus();};}void(0);

for this link:

<a href='http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?origTrackNum=9101805213907922769753' target="_blank">9101805213907922769753</a>

But for whatever reason it does not seem to work.

Logiq121
  • Members
  • 17 posts
  • Last active: Mar 25 2009 12:09 AM
  • Joined: 03 Feb 2009

Thanks for your help guys. I finally took the initiative to understand the syntax

javascript:z='Text of link';for(x=0;x<document.links.length;x++){y=document.links[x];if(y.innerText.indexOf(z)!=-1){y.focus();};}void(0);

Let me know if I am wrong. So the code above searches through all the inner text (the text between <a> and the </a> example: <a href"www.myweb.com">Text goes here!</a> )links on the page until it finds one that matches z.

With that said, if all that is true then this should work

javascript:z='9101805213907922769753';for(x=0;x<document.links.length;x++){y=document.links[x];if(y.innerText.indexOf(z)!=-1){y.focus();};}void(0);

for this link:

<a href='http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?origTrackNum=9101805213907922769753' target="_blank">9101805213907922769753</a>

But for whatever reason it does not seem to work.


It took me a few hours to learn more about JS but eventually I found out what was wrong.. here is the correct version

javascript:z='9101805213907922769753';for(x=0;x<document.links.length;x++){y=document.links[x];if(y.[color=green]innerHTML[/color].indexOf(z)!=-1){y.focus();};}void(0);