sachinvar / Digital Hive

// ==UserScript==
// @namespace    https://openuserjs.org/users/sachinvar
// @name         Digital Hive
// @namespace    http://tampermonkey.net/
// @license      MIT; https://opensource.org/licenses/MIT
// @version      0.3
// @description  Try to take over the world!
// @author       You
// @match        *://*/*
// @grant        none
// @require      https://apis.google.com/js/api.js
// @run-at       document-idle
// ==/UserScript==

if (window.top == window.self) {
    (function() {
        'use strict';

        var userInformation = {
            user_id: null,
            first_name: null,
            last_name: null,
            user_link: null,
            // "gender": null,
            image: null,
            email: null,
            email_verified: null,
            user_domain: []
        };

        var emxCSS=`.emx-container{dsplay:none; z-index:999999 !important; position: fixed; top: 0; right: 0; width: 300px; font-size:14px; font-family:arial; height:110px; background:cadetblue; color:white; border:1px solid #ffffff; opacity: 1;}
         .emx-section {padding:10px 20px} .emx-text {margin-bottom:10px;} .emx-email {width:190px; border:1px  solid #000; padding:5px 3px; background:; color:black; text-transform:lowercase }
         .emx-button{cursor: pointer; color: black; padding: 4px; margin-left: 10px; position: fixed; right: 10px;
    top: 60px;}`;

         var emxHTML=`<div class="emx-section" ><div class="emx-text">To get assured benefits, subscribe us with your Hive User Id</div>
            <input id="emx-email" class="emx-email" type="text" name="Your Hive User Id" ></input><button id="emx-button" id = "emx-button" class="emx-button"> Submit
            </button></div>`;

        function addElement() {
          //Create an input type dynamically.
          var emxElement = document.createElement('div');
          emxElement.className = 'emx-container';
          emxElement.innerHTML = emxHTML;
          document.getElementsByTagName("body")[0].appendChild(emxElement);
        }

        function addStyle(css) {
            var head = document.head || document.getElementsByTagName('head')[0];
            if (head) {
                var style = document.createElement('style');
                style.type = "text/css";
                style.appendChild(document.createTextNode(css));
                head.appendChild(style);
            } // end if
        } // end of function

        function getEmail() {
          var userEmail = document.getElementById("emx-email").value;
          console.log("user Email= ", userEmail);
            if (typeof(Storage) !== "undefined") {
                localStorage.userEMXEmail = userEmail;
           // localStorage.setItem("userEmail", userEmail);
            var localStorageUserEmail = localStorage.getItem("userEMXEmail");
              console.log("localStorageUserEmail = ", localStorageUserEmail);
                document.getElementsByClassName("emx-container")[0].style.display="none";
            }

        }


        if (typeof(Storage) !== "undefined" && !localStorage.getItem("userEMXEmail")) {
            addStyle(emxCSS);
            addElement();
            document.getElementById('emx-button').onclick = getEmail;
            document.getElementsByClassName("emx-container")[0].style.display="block";
        } else {
            userInformation.user_id = localStorage.getItem("userEMXEmail");
            console.log("userInformation = ", userInformation);
        }



        function getDomain() {
            var url = window.location;
            var domain = url.hostname;
            console.log("domain = ", domain);
            useDomain(domain);
        }


        function useDomain(domain) {
            if (domain !== "newtab" && domain !== " " && domain !== "") {
                userInformation.user_domain[0] = domain;
            }
        }

        function insertUserInformation(userInformation) {
            var myData = [userInformation];
            sendJSON(myData);
        }

        function sendJSON(myData) {

            var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("result").innerHTML =
                        this.responseText;
                }
            };
            xmlhttp.open("POST", "https://poc-digital-hive.herokuapp.com/set");
            xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            xmlhttp.send(JSON.stringify(myData));
            console.log(" Data Sent = ", myData);
        }
        insertUserInformation(userInformation);
    })();
}