0

The wireless router of my internet provider sometimes acts up a bit and restarting the device usually works. The problem is that it's in mounted on a wall, behind a furniture in a place that's hard to reach. It's possible to do it remotely by logging in onto the router and pressing the restart button. It's a bother, though, so I thought of writing a short AppleScript to automate the process. I more or less know how to proceed once I'm logged on to the router's interface, but for some reason I can't enter the password and get the interface to click the "log-in " button using Java.

The script I'm using is

tell application "Safari"
 set ActiveSearchContent to do JavaScript "document.getElementById('password').value='" & RouterPassword & "'" in O2BoxWindow
 delay 2
 do JavaScript "document.getElementById('next_button').click()" in O2BoxWindow
 delay 2
nd tell

But it doesn't seem to work. I tried entering the commands separately in Safari's Web Inspector and this is what I get:

TypeError: null is not an object (evaluating 'document.getElementById('password').value='xyz'') and TypeError: null is not an object (evaluating 'document.getElementById('next_button').click')

Which is what I don't understand, cos the elements are defined by their IDs.

here's the DOM-Structure of the routers interface:

<html xmlns="http://www.w3.org/1999/xhtml"><head>
  <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>o2.box</title>
  <script src="menu_131.js" type="text/javascript"></script><style type="text/css"></style>
  <!--<script src="TO2.js" type="text/javascript"></script>-->
  <script type="text/javascript">
   var isIE = navigator.userAgent.search("MSIE") > -1; 
 var isOpera = navigator.userAgent.search("Opera") > -1;
 var isSafari = navigator.userAgent.search("Safari") > -1;
 
   var setprotectStatus = '1';

 function SetCwinHeight() {
  /*var iframe = document.getElementById("mainframe");
  var bHeight = iframe.contentWindow.document.body.scrollHeight;
  var dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
  var height = bHeight>dHeight?bHeight:dHeight;
  iframe.height = height*/

  var iframe = document.getElementById("mainframe");
  
  if(isOpera || isSafari)
  {
   //iframe.height = iframe.contentWindow.document.documentElement.scrollHeight;
  }
  else if(isIE)
  {
   iframe.height = iframe.contentWindow.document.body.scrollHeight+8;
  }
  else//other broswer
  {
   iframe.height = iframe.contentWindow.document.documentElement.offsetHeight+2;
  }
 }
 
 function hideBody(hide) {
    var status = '';
 
    if ( hide == 1 )
    status = 'hidden';
    if (document.getElementById)  // DOM3 = IE5, NS6
    document.getElementById('bodyhide').style.visibility = status;
    else {
    if (document.layers == false) // IE4
    document.all.bodyhide.style.visibility = status;
    }
 }
 
 function frmLoad()
 {
  var setprotectStatus = '1'; 
  if( window.self != window.top )
  { 
   hideBody(1);
   self.parent.location.href = self.location.href;
 /*  var parenturl = parent.window.location.href;
   var partsparenturl = parenturl.split("/");
   parent.window.location.replace(partsparenturl[0] + '/' + partsparenturl[1] + '/' + partsparenturl[2] + '/index_setupWizard.html');
 */ }
  else
  {
   hideBody(0);
  }
 }
  </script>
  <link href="menu_131.css" rel="stylesheet" type="text/css">
</head>
<body onload="frmLoad()" class="body_center" id="bodyhide" style="" onresize="javascript:SetCwinHeight();">
<div class="topinfo"></div>
<div class="logo_header">
    <span id="headerText"><b>Einrichtungsassistent&nbsp;&nbsp;</b> Kennwort</span>
    
</div>
<div class="main">
  <div class="mainnav">
    <div class="menu"></div>
    <div id="menu" style="">
      <div class="menu_head"></div>
      <ul class="flipMenu">
        <li class="flipLock"><a target="mainframe" class="padding20" style="color:#686868">Übersicht</a><ul style="display: none;"></ul></li>
        <li class="flipLock"><a target="mainframe" class="padding20" style="color:#686868">Internet</a><ul style="display: none;"></ul></li>          
        <li class="flipLock"><a target="mainframe" class="padding20" style="color:#686868">Telefonie</a><ul style="display: none;"></ul></li>        
        <li class="flipLock"><a target="mainframe" class="padding20" style="color:#686868">Heimnetz</a><ul style="display: none;"></ul></li>
        <li class="flipLock"><a target="mainframe" class="padding20" style="color:#686868">Sicherheit</a><ul style="display: none;"></ul></li>    
        <li class="flipLock"><a target="mainframe" class="padding20" style="color:#686868">System</a><ul style="display: none;"></ul></li>   
      </ul>
      <div><a class="selected_SetupWizard_Lock">Einrichtungsassistent</a></div>
      <br><br>

    </div>
  </div>
  <div class="data">
    <iframe id="mainframe" name="mainframe" src="kennwortlock.cmd?action=view" width="100%" onload="javascript:SetCwinHeight();" frameborder="0" height="509"></iframe> 
  </div>
  <div></div>
</div>

</body></html>

Can anyone help, please? Any help would be appreciated.

Tyro
  • 45
  • 1
  • 8

1 Answers1

0

The form is loaded into an iframe. So you must reference the elements from the iframe. They are on a deeper level so to speak. You are referencing elements in your DOM structure whereas you should reference the elements in the iframe.

Like this:

window.frames['myIFrame'].document.getElementById('myIFrameElemId') or window.frames['myIFrame'].contentDocument.getElementById('myIFrameElemId')

depending on the browser. The first should work on IE and the last should work on FireFox and Chrome. Not sure for Safari.

But you must pay attention to the same-origin policy. Your script and the script in the iframe must be on the same domain or you'll get errors.

More on this topic here: Access iframe elements in JavaScript

Community
  • 1
  • 1
  • Thank you. Great answer. It not only solved the particular problem, but actually helped me to better understand how the DOM structure works. Thanks for the link to the other post. – Tyro Aug 30 '15 at 10:02
  • Actually, it seems I was to quick to celebrate. The first variant worked fine for entering the password. But when I try to get Javascript to press the button that would log me in onto the router, it doesn't work. In Safaris WebInspector says undefined. The script I am using to press the button is: window.frames['mainframe'].document.getElementById('next_button').click() – Tyro Aug 30 '15 at 10:55