Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*TODO:
- *-click and test button in doLogin (clicks multiple times currently, infinite upon bad login)
- *-is saveURL necessary?
- *-fix minor issue with nothing stored in lastURL
- *-fix background image in div
- *-get loginsuccess flag somehow
- */
- document.body.insertAdjacentHTML("afterbegin", "<webview id='myView' src='http://www.shoplogix.com'></webview>");
- //document.body.insertAdjacentHTML("afterbegin", "<div class='image'>use ctrl-s to toggle form</div>");
- var webview = document.querySelector("webview");
- var button = document.getElementById("myButton");
- var myForm = document.getElementById("input");
- //var mydiv = document.getElementsByClassName("image")[0];
- //var loginSuccess = document.getElementById("whiteboard");
- var ctrlOn;
- var formVisible;
- var isMobile;
- var key = "hello_world";
- var url;
- var user;
- var pw;
- var divShown = false; //true if div is shown, false if webview is shown
- //var divShown = true;
- //resizes div to fill entire screen
- /*
- function updateDiv() {
- webview.style.height = 0;
- webview.style.width = 0;
- mydiv.style.height = document.documentElement.clientHeight + "px";
- mydiv.style.width = document.documentElement.clientWidth + "px";
- formVisible = false;
- divShown = true;
- }
- */
- //resizes the webview to fill entire app screen
- function updateWebview() {
- if (!isMobile) {
- //mydiv.style.height = 0;
- //mydiv.style.width = 0;
- webview.style.height = document.documentElement.clientHeight + "px";
- webview.style.width = document.documentElement.clientWidth + "px";
- }
- formVisible = false;
- //divShown = false;
- };
- window.onload = function() {
- try {
- if (cordova.platformId.length > 1) {
- isMobile = true;
- }
- } catch (e) {
- isMobile = false;
- }
- updateWebview();
- //updateDiv();
- chrome.storage.local.get(["lastURL", "user", "pass"], function(result) {
- if (result.lastURL.length > 7) {
- webview.setAttribute("src", formatURL(result.lastURL));
- doLogin(CryptoJS.AES.decrypt(result.user, key).toString(CryptoJS.enc.Utf8),
- CryptoJS.AES.decrypt(result.pass, key).toString(CryptoJS.enc.Utf8));
- }
- });
- }
- window.onresize = updateWebview(); //TODO deal with this with div update
- //show/hide the [url, username, pw] input form upon ctrl-s being pressed
- $(document).ready(function() {
- $(document).keyup(function(e) {
- if (e.which == 17) {
- ctrlOn = false;
- }
- });
- $(document).keydown(function(e) {
- if (e.which == 17) {
- ctrlOn = true;
- }
- if (ctrlOn && e.which == 83) {
- if (formVisible) {
- /*
- if (divShown) {
- updateDiv();
- } else {
- updateWebview();
- }
- */
- updateWebview();
- } else {
- /*
- if (divShown) {
- mydiv.style.height = document.documentElement.clientHeight - 58 + "px";
- } else {
- webview.style.height = document.documentElement.clientHeight - 50 + "px";
- }*/
- webview.style.height = document.documentElement.clientHeight - 50 + "px";
- formVisible = true;
- }
- }
- });
- });
- //gets user input for new url after submit is clicked
- document.addEventListener("DOMContentLoaded", function() {
- button.addEventListener("click", function() {
- url = myForm.elements[0].value;
- user = myForm.elements[1].value;
- pw = myForm.elements[2].value;
- var saveURL = url;
- if (url.length > 0 && user.length > 0 && pw.length > 0) {
- url = formatURL(url);
- } else {
- return;
- }
- webview.setAttribute("src", url);
- doLogin(user, pw);
- myForm.reset();
- //encrypts and saves form data
- chrome.storage.local.set({"lastURL": saveURL});
- chrome.storage.local.set({"user": CryptoJS.AES.encrypt(user, key)});
- chrome.storage.local.set({"pass": CryptoJS.AES.encrypt(pw, key)});
- });
- });
- //format url appends whiteboard to url if necessary.
- function formatURL(url) {
- var hasHttp = false;
- //check for http/https at the beginning, and remove if necessary
- if (url.substring(0, 7) == "http://") {
- url = url.substring(7, url.length);
- } else if (url.substring(0, 8) == "https://") {
- url = url.substring(8, url.length);
- }
- //check for /whiteboard/ at the end, and append if necessary
- if (url.substring(url.length-11, url.length) != "whiteboard/" && url.substring(url.length-10, url.length) != "whiteboard") {
- if (url.substring(url.length-1, url.length) == "/") {
- url = url + "whiteboard";
- } else {
- url = url + "/whiteboard";
- }
- }
- url = "https://" + url;
- //Removes possible trailing slash
- if (url.substring(url.length-1,url.length) == "/") {
- return url.substring(0,url.length-1);
- } else {
- return url;
- //return;
- }
- }
- //doLogin fills in the form if necessary (right login page, and only clicks button once (upon a potential failed password))
- function doLogin(user, pw) {
- webview.addEventListener("loadstop", function() {
- webview.executeScript({code: "var user = '"+user+"';"
- +"var pass = '"+pw+"';"
- +"var userField = document.getElementById('username');"
- +"var passField = document.getElementById('password');"
- +"var errorMsg = document.getElementsByClassName('alert-danger')[0];"
- +"if (userField) {"
- +"console.log(1,userField.value);"
- +"userField.value = user;"
- +"passField.value = pass;"
- +"document.querySelector('button').innerHTML = 'hello';"
- +"console.log(2,userField.value);"
- //+"document.querySelector('button').click();"
- //+"console.log(error);"
- //+"if (!error) {document.querySelector('button').click();}" //okay this sorta works but clicks alot
- +"}"
- //TODO: Overlay, and prompt user to enter another user/pass, check if valid again
- // +"} else {"
- // +"while (userField && errorMsg) {"
- // +"document.querySelector('button').innerHTML = 'failed';"
- // +"} }"
- }, function() {
- console.log("Script executed");
- /*if (loginSuccess) {
- updateWebview();
- } else {
- updateDiv();
- }*/
- updateWebview();
- /*
- if (divShown) {
- updateDiv();
- } else {
- updateWebview();
- }
- console.log(loginSuccess);
- */
- });
- });
- }
- /*
- https://stackoverflow.com/questions/42692472/visual-studio-2017-error-unable-to-start-program-an-operation-is-not-legal-in
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement