Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*TODO:
  2.  *-click and test button in doLogin (clicks multiple times currently, infinite upon bad login)
  3.  *-is saveURL necessary?
  4.  *-fix minor issue with nothing stored in lastURL
  5.  *-fix background image in div
  6.  *-get loginsuccess flag somehow
  7.  */
  8.  
  9. document.body.insertAdjacentHTML("afterbegin", "<webview id='myView' src='http://www.shoplogix.com'></webview>");
  10. //document.body.insertAdjacentHTML("afterbegin", "<div class='image'>use ctrl-s to toggle form</div>");
  11.  
  12. var webview = document.querySelector("webview");
  13. var button = document.getElementById("myButton");
  14. var myForm = document.getElementById("input");
  15. //var mydiv = document.getElementsByClassName("image")[0];
  16. //var loginSuccess = document.getElementById("whiteboard");
  17. var ctrlOn;
  18. var formVisible;
  19. var isMobile;
  20. var key = "hello_world";
  21. var url;
  22. var user;
  23. var pw;
  24. var divShown = false; //true if div is shown, false if webview is shown
  25. //var divShown = true;
  26.  
  27. //resizes div to fill entire screen
  28. /*
  29. function updateDiv() {
  30.     webview.style.height = 0;
  31.     webview.style.width = 0;
  32.     mydiv.style.height = document.documentElement.clientHeight + "px";
  33.     mydiv.style.width = document.documentElement.clientWidth + "px";
  34.     formVisible = false;
  35.     divShown = true;
  36. }
  37. */
  38. //resizes the webview to fill entire app screen
  39. function updateWebview() {
  40.     if (!isMobile) {
  41.         //mydiv.style.height = 0;
  42.         //mydiv.style.width = 0;
  43.         webview.style.height = document.documentElement.clientHeight + "px";
  44.         webview.style.width = document.documentElement.clientWidth + "px";
  45.     }
  46.   formVisible = false;
  47.     //divShown = false;
  48. };
  49. window.onload = function() {
  50.   try {
  51.         if (cordova.platformId.length > 1) {
  52.             isMobile = true;
  53.         }
  54.     } catch (e) {
  55.         isMobile = false;
  56.     }
  57.     updateWebview();
  58.     //updateDiv();
  59.   chrome.storage.local.get(["lastURL", "user", "pass"], function(result) {
  60.     if (result.lastURL.length > 7) {
  61.       webview.setAttribute("src", formatURL(result.lastURL));
  62.       doLogin(CryptoJS.AES.decrypt(result.user, key).toString(CryptoJS.enc.Utf8),
  63.       CryptoJS.AES.decrypt(result.pass, key).toString(CryptoJS.enc.Utf8));
  64.     }
  65.   });
  66. }
  67. window.onresize = updateWebview(); //TODO deal with this with div update
  68.  
  69.  
  70. //show/hide the [url, username, pw] input form upon ctrl-s being pressed
  71. $(document).ready(function() {
  72.     $(document).keyup(function(e) {
  73.         if (e.which == 17) {
  74.             ctrlOn = false;
  75.         }
  76.     });
  77.     $(document).keydown(function(e) {
  78.         if (e.which == 17) {
  79.             ctrlOn = true;
  80.         }
  81.         if (ctrlOn && e.which == 83) {
  82.         if (formVisible) {
  83.                 /*
  84.                 if (divShown) {
  85.                     updateDiv();
  86.                 } else {
  87.                     updateWebview();
  88.                 }
  89.                 */
  90.                 updateWebview();
  91.         } else {
  92.                 /*
  93.                 if (divShown) {
  94.                     mydiv.style.height = document.documentElement.clientHeight - 58 + "px";
  95.                 } else {
  96.                     webview.style.height = document.documentElement.clientHeight - 50 + "px";
  97.                 }*/
  98.                 webview.style.height = document.documentElement.clientHeight - 50 + "px";
  99.           formVisible = true;
  100.         }
  101.         }
  102.     });
  103. });
  104.  
  105. //gets user input for new url after submit is clicked
  106. document.addEventListener("DOMContentLoaded", function() {
  107.     button.addEventListener("click", function() {
  108.         url = myForm.elements[0].value;
  109.         user = myForm.elements[1].value;
  110.         pw = myForm.elements[2].value;
  111.         var saveURL = url;
  112.         if (url.length > 0 && user.length > 0 && pw.length > 0) {
  113.             url = formatURL(url);
  114.         } else {
  115.             return;
  116.         }
  117.  
  118.         webview.setAttribute("src", url);
  119.     doLogin(user, pw);
  120.         myForm.reset();
  121.  
  122.         //encrypts and saves form data
  123.         chrome.storage.local.set({"lastURL": saveURL});
  124.         chrome.storage.local.set({"user": CryptoJS.AES.encrypt(user, key)});
  125.         chrome.storage.local.set({"pass": CryptoJS.AES.encrypt(pw, key)});
  126.     });
  127. });
  128.  
  129.  
  130. //format url appends whiteboard to url if necessary.
  131. function formatURL(url) {
  132.   var hasHttp = false;
  133.     //check for http/https at the beginning, and remove if necessary
  134.     if (url.substring(0, 7) == "http://") {
  135.         url = url.substring(7, url.length);
  136.     } else if (url.substring(0, 8) == "https://") {
  137.         url = url.substring(8, url.length);
  138.     }
  139.  
  140.     //check for /whiteboard/ at the end, and append if necessary
  141.     if (url.substring(url.length-11, url.length) != "whiteboard/" && url.substring(url.length-10, url.length) != "whiteboard") {
  142.         if (url.substring(url.length-1, url.length) == "/") {
  143.             url = url + "whiteboard";
  144.         } else {
  145.             url = url + "/whiteboard";
  146.         }
  147.     }
  148.   url = "https://" + url;
  149.     //Removes possible trailing slash
  150.     if (url.substring(url.length-1,url.length) == "/") {
  151.         return url.substring(0,url.length-1);
  152.     } else {
  153.         return url;
  154.         //return;
  155.     }
  156. }
  157. //doLogin fills in the form if necessary (right login page, and only clicks button once (upon a potential failed password))
  158. function doLogin(user, pw) {
  159.   webview.addEventListener("loadstop", function() {
  160.     webview.executeScript({code: "var user = '"+user+"';"
  161.     +"var pass = '"+pw+"';"
  162.     +"var userField = document.getElementById('username');"
  163.     +"var passField = document.getElementById('password');"
  164.         +"var errorMsg = document.getElementsByClassName('alert-danger')[0];"
  165.     +"if (userField) {"
  166.         +"console.log(1,userField.value);"
  167.     +"userField.value = user;"
  168.     +"passField.value = pass;"
  169.     +"document.querySelector('button').innerHTML = 'hello';"
  170.         +"console.log(2,userField.value);"
  171.         //+"document.querySelector('button').click();"
  172.         //+"console.log(error);"
  173.         //+"if (!error) {document.querySelector('button').click();}" //okay this sorta works but clicks alot
  174.         +"}"
  175.         //TODO: Overlay, and prompt user to enter another user/pass, check if valid again
  176.     // +"} else {"
  177.         // +"while (userField && errorMsg) {"
  178.         // +"document.querySelector('button').innerHTML = 'failed';"
  179.         // +"} }"
  180.         }, function() {
  181.       console.log("Script executed");
  182.             /*if (loginSuccess) {
  183.                 updateWebview();
  184.             } else {
  185.                 updateDiv();
  186.             }*/
  187.             updateWebview();
  188.             /*
  189.             if (divShown) {
  190.                 updateDiv();
  191.             } else {
  192.                 updateWebview();
  193.             }
  194.             console.log(loginSuccess);
  195.             */
  196.     });
  197.   });
  198. }
  199.  
  200.  
  201. /*
  202. https://stackoverflow.com/questions/42692472/visual-studio-2017-error-unable-to-start-program-an-operation-is-not-legal-in
  203. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement