Advertisement
rccharles

auto login and signin

Mar 23rd, 2017
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name               ASC auto login and signin  
  3. // @namespace        http://www.squarefree.com/userscripts
  4. // @description    Automatically submit autofilled sigin forms
  5. // @include        https://idmsa.apple.com/appleauth*
  6. // @include          https://idmsa.apple.com/IDMSWebAuth/login?appIdKey=*
  7. // @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
  8. // @grant          none
  9. // @version        15Mar2017
  10. // ==/UserScript==
  11.  
  12. /*
  13.    More dump routines
  14.    http://stackoverflow.com/questions/2934787/view-list-of-all-javascript-variables-in-google-chrome-console
  15.  */
  16. var signinTries = 0;
  17.  
  18. /* ====================================================================== */
  19. /* login to the old Style ASC password web page */
  20. /* Code from GreaseMonkey Hacks: Tips & Tools for Remixing the
  21.    Web with Firefox by Mark Pilgrim  */
  22.  
  23. function submitFirstPasswordForm()
  24. {
  25.    if (debug) console.log("in submitFirstPasswordForm.")
  26.  
  27.    for (var elmForm, i=0; elmForm=document.forms[i]; ++i)
  28.    {
  29.      var numPasswordElements = 0;
  30.  
  31.      for (var j=0; elmFormElement=elmForm.elements[j]; ++j)
  32.      {
  33.        if (debug) console.log("elmFormElement.type is " + elmFormElement.type);
  34.        
  35.        if (elmFormElement.type == "password" &&
  36.            elmFormElement.value &&
  37.            elmFormElement.value.toLowerCase() != "password")
  38.          {
  39.             ++numPasswordElements;
  40.          }
  41.      } /* of for j */
  42.  
  43.      if (numPasswordElements != 1)
  44.        { continue; /* not something we are prepared to handle. Next i */}  
  45.      
  46.      if (debug) console.log("found password.")
  47.      /*
  48.       * The obvious way to submit a login form is form.submit().   
  49.       * However, this doesn't work with some forms, such as
  50.       * the Google AdWords login, because they do stuff
  51.       * in the onclick handler of the submit button. So we
  52.       * need to find the submit button and simulate a click.
  53.       */
  54.      var elmSubmit = document.evaluate(".//input[@type='image']",
  55.                                        elmForm,
  56.                                        null,
  57.                                        XPathResult.FIRST_ORDERED_NODE_TYPE,
  58.                                        null).singleNodeValue;
  59.      if (!elmSubmit)
  60.        {
  61.          elmSubmit = document.evaluate(".//input[@type='submit']",
  62.                                        elmForm,
  63.                                        null,
  64.                                        XPathResult.FIRST_ORDERED_NODE_TYPE,
  65.                                        null).singleNodeValue;
  66.        }      
  67.      if (!elmSubmit)
  68.        { continue; }
  69.      /*
  70.      * Give a visual indication that we're logins to web sitesauto-submitting the
  71.      * form, then simulate a click on the submit button.
  72.      */
  73.         var zero = 0;
  74.         if (debug) console.log("clicking...");
  75.         var z = 0;
  76.         elmSubmit.focus();
  77.         /* Doesn't seem to last long enough for use to observe */
  78.         elmSubmit.style.MozOutline = "2px solid purple";
  79.        
  80.         elmSubmit.click();
  81.      
  82.      } /* end of for i */
  83.   } /* end of submitFirstPasswordForm */
  84.  
  85.  
  86.  
  87. /* ====================================================================== */
  88. /* when ASC signin password is present, click on the arrow icon to signin */
  89. function submitSignin()
  90. {
  91.  if (debug) console.log("in function submitSignin...");
  92.  
  93.  signinTries++
  94.  /* Wait for the iFrame javascript to run and for firefox to fill in the password. */
  95.  if (signinTries> 9)
  96.    {
  97.      if (debug) console.log("done trying.");
  98.      return; // we are done trying. Let the user proced manually.
  99.    }
  100.  
  101.  if (debug) console.log("signinTries= " + signinTries.toString());
  102.  try
  103.   {
  104.     /* iframe javascript fills in password fields. We wait for pwd. */
  105.     /* firefox fills in password */
  106.     if ( document.getElementById("pwd").value == '')
  107.     {
  108.       /* We know Firefox hasn't filled in password fields in iframe */
  109.       if (debug) console.log("wait on Firefox bit.");
  110.       setTimeout(submitSignin, 100);
  111.      
  112.       return;  
  113.     }
  114.    
  115.   }
  116.   catch (e)
  117.   {
  118.       /* The signin page uses Javascript code to inject the password.  Wait another bit.
  119.        */
  120.         if (debug) console.log("wait a bit.");
  121.         setTimeout(submitSignin, 100);
  122.    
  123.         return;
  124.   }
  125.  
  126.   /* All present and accounted for. */
  127.   /* click on the signin icon */
  128.   try
  129.   {  
  130.     elmSubmit = document.getElementById("sign-in")
  131.     if (debug) console.log("elmSubmit " + elmSubmit);
  132.     elmSubmit.focus();
  133.     /* Doesn't seem to last long enough for use to observe */
  134.     elmSubmit.style.MozOutline = "2px solid purple";
  135.     if (debug) console.log("'Click' on signin icon");
  136.     elmSubmit.click();
  137.   }
  138.   catch (e)
  139.   {
  140.     console.log("failure with elmSubmit..."+e.message);  
  141.   }
  142.  
  143. } /* End of function submitSignin */
  144.  
  145.  
  146.  
  147. /* =================== first line of code ================================ */
  148. var aDate = new Date();
  149. console.log ("--> Check for sigin and login with password. "  +
  150.              " on " + aDate );
  151.  
  152. if (debug<=2) console.log("document.domain is " + document.domain );
  153. /* Returns the location of the iFrame.  */
  154. if (debug) console.log("window.location.href:\n" + window.location.href );
  155.  
  156. // Distinguish between the older ASC login and the newer ASC signin
  157. // We get the url of the iFrame not the signin url
  158. /*
  159.     https://idmsa.apple.com/appleauth/auth/signin?widgetKey=
  160.     https://idmsa.apple.com/IDMSWebAuth/login?appIdKey=
  161.     012345678901234567890123456789012345678901234567890123456789
  162.              1         2         3         4         5 */
  163.   var res = window.location.href.substr(0, 51);
  164.   if (debug) console.log("page substring:\n" + res );
  165.   if ( res ==     "https://idmsa.apple.com/appleauth/auth/signin?widge")
  166.   {
  167.       if (debug) console.log ("--> Signin");  
  168.       /* The signin page uses Javascript code to inject the password prompt.  Wait for the
  169.          code to complet. */
  170.       window.addEventListener("load", function() {
  171.         if (debug) console.log ("--> load event");  
  172.         /*
  173.         * Using setTimeout to give Firefox's password manager a chance 
  174.         * to autofill the form.
  175.         */
  176.         setTimeout(submitSignin, 100);
  177.        
  178.         },  false);
  179.   }
  180.   else if (res == "https://idmsa.apple.com/IDMSWebAuth/login?appIdKey=")
  181.   {
  182.     if (debug) console.log ("--> Login");
  183.  
  184.  
  185.     window.addEventListener("load", function() {
  186.     /*
  187.      * Using setTimeout to give Firefox's password manager a chance
  188.      * to autofill the form.
  189.      */
  190.     setTimeout(submitFirstPasswordForm, 0);
  191.       }, false);
  192.   }
  193.   else
  194.   {
  195.      console.log ("--> Unsupported web site!");
  196.      alert("--> Unsupported web site! Ignoring.")
  197.   }
  198.  
  199.  
  200.  
  201. if (debug) console.log("...after sorting web site ... ");
  202.  
  203. /* Hocus Pocus */
  204. var done = 1;
  205. /* make invoker happy */
  206. done = 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement