");
-
- /**
- * Generate a random alphanumeric string
- * http://stackoverflow.com/questions/10726909/random-alpha-numeric-string-in-javascript
- */
- function randomString(length) {
- var result = '';
- var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
-
- for(var i = length; i > 0; --i) {
- result += chars[Math.floor(Math.random() * chars.length)];
- }
-
- return result;
- }
-
- /**
- * Push the page down to make room for notifications
- */
- function updateTopMargin() {
- $("html").css("margin-top", (notifications_box.is(":visible") ? notifications_box.outerHeight() : 0));
- }
-
- return {
- // Randomized CSS class names
- gh_main_container: randomString(10),
- gh_button: randomString(10),
- gh_button_on: randomString(10),
- gh_button_off: randomString(10),
- gh_notification_container: randomString(10),
- gh_notification: randomString(10),
- gh_error: randomString(10),
- gh_close: randomString(10),
-
- /**
- * Print the UI
- */
- loadUI: function() {
- GM_addStyle(`
- html {
- overflow-y: scroll !important;
- }
-
- .${this.gh_notification_container} {
- font-size: 16.5px;
- left: 0px;
- position: fixed;
- text-align: center;
- top: 0px;
- width: 100%;
- z-index: 9999999999;
- }
-
- .${this.gh_notification} {
- background: #000;
- border-top: 1px solid rgba(52, 152, 219, .5);
- box-shadow: 0px 2px 10px rgba(0, 0, 0, .5);
- box-sizing: border-box;
- color: #3498db;
- line-height: 22px;
- padding: 12px;
- width: 100%;
- }
-
- .${this.gh_error} {
- background: #e74c3c;
- border-top: 1px solid rgba(255, 255, 255, .5);
- box-shadow: 0px 2px 10px rgba(231, 76, 60, .5);
- box-sizing: border-box;
- color: #fff;
- line-height: 22px;
- padding: 12px;
- width: 100%;
- }
-
- .${this.gh_error} a {
- color: #fff;
- }
-
- .${this.gh_close} {
- float: right;
- background: rgba(255, 255, 255, .15);
- border: 1px solid #fff;
- box-shadow: 0px 0px 8px rgba(255, 255, 255, .5);
- cursor: pointer;
- margin-left: 4px;
- padding: 0px 4px;
- }
-
- .${this.gh_close}:hover {
- background: #fff;
- color: #e74c3c;
- }
-
- .${this.gh_close}::before {
- content: 'x';
- position: relative;
- top: -1px;
- }
- `);
-
- notifications_box.addClass(this.gh_notification_container);
- $("body").append(notifications_box);
-
- },
-
- /**
- * Print an error
- */
- showError: function(msg) {
- // Don't print the same error multiple times
- if(active_errors.indexOf(msg) != -1) return
-
- var self = this;
-
- active_errors.push(msg);
-
- notifications_box.append(
- $("
", { class: this.gh_error }).html(`
Giveaway Helper Error: ${msg}`).prepend(
- $("
", { class: this.gh_close }).click(function() {
- $(this).unbind("click");
- $(this).parent().slideUp(400, function() {
- active_errors.splice(active_errors.indexOf(msg), 1);
- $(this).remove();
- updateTopMargin();
- });
- })
- ));
-
- updateTopMargin();
- },
-
- /**
- * Display or update a notification
- */
- showNotification: function(notification_id, msg, hide_delay) {
- if(!active_notifications[notification_id]) {
- // New notification
- active_notifications[notification_id] = $("
", { class: this.gh_notification });
- notifications_box.append(active_notifications[notification_id]);
- }
-
- // Update notification
- active_notifications[notification_id].html(`
Giveaway Helper Notification: ${msg}`);
- updateTopMargin();
-
- // Automatically hide notification after a delay
- if(typeof hide_delay == "number") {
- var self = this;
- setTimeout(function() {
- self.hideNotification(notification_id);
- }, hide_delay);
- }
- },
-
- /**
- * Remove a notification
- */
- hideNotification: function(notification_id) {
- if(active_notifications[notification_id]) {
- var old_notification = active_notifications[notification_id];
-
- delete active_notifications[notification_id];
- old_notification.slideUp(400, function() {
- old_notification.remove();
- updateTopMargin();
- });
- }
- },
-
- /**
- *
- */
- buildButton: function(button_id, label, button_on, click_function) {
- var new_button =
- $("
", {
- class: `${this.gh_button} ${button_on ? this.gh_button_on : this.gh_button_off} btn`
- }).append(
- $("", { text: label })).append(
- $("", { class: "fa ng-scope fa-refresh fa-spin", css: { display: "none" }})
- ).click(function(e) {
- e.stopPropagation();
- if(!active_buttons[button_id].find(".fa").is(":visible")) {
- click_function();
- }
- });
-
- active_buttons[button_id] = new_button;
- return new_button;
- },
-
- /**
- *
- */
- removeButton: function(button_id) {
- active_buttons[button_id].remove();
- delete active_buttons[button_id];
- },
-
- /**
- *
- */
- setButtonLabel: function(button_id, label, color) {
- active_buttons[button_id].find("span").first().text(label);
-
- if(color !== undefined) {
- active_buttons[button_id].css("background-color", color);
- active_buttons[button_id].css("border-color", color);
- }
- },
-
- /**
- *
- */
- toggleButtonClass: function(button_id) {
- active_buttons[button_id].toggleClass(this.gh_button_on);
- active_buttons[button_id].toggleClass(this.gh_button_off);
- },
-
- /**
- *
- */
- showButtonLoading: function(button_id) {
- active_buttons[button_id].find("span").first().hide();
- active_buttons[button_id].find(".fa").show();
- },
-
- /**
- *
- */
- hideButtonLoading: function(button_id) {
- active_buttons[button_id].find("span").first().show();
- active_buttons[button_id].find(".fa").hide();
- }
- };
- })();
-
- /**
- * Used to communicate with and run code on a different domain
- * Usualy with the intent to grab necessary cookies
- */
- var commandHub = (function() {
- /**
- * http://stackoverflow.com/a/15724300
- */
- function getCookie(name) {
- var value = "; " + document.cookie,
- parts = value.split("; " + name + "=");
-
- if(parts.length == 2) {
- return parts.pop().split(";").shift();
- } else {
- return null;
- }
- }
-
- return {
- /**
- * Load an iframe so that we can run code on a different domain
- * @param {String} url - The url to be loaded into the iframe
- * @param {Function} data_func - The code that we're going to run inside the iframe
- * @param {Function} callback - Runs after data_func returns
- */
- load: function(url, hostname, data_func, callback) {
- var command_hub = document.createElement('iframe');
-
- command_hub.style.display = "none";
- command_hub.src = url;
- document.body.appendChild(command_hub);
-
- hostname = hostname.replace(/\./g, "_");
-
- var funcvar = `command_hub_func_${hostname}`,
- retvar = `command_hub_return_${hostname}`;
-
- window.addEventListener("message", function(event) {
- if(event.source == command_hub.contentWindow) {
- if(event.data.status == "ready") {
- // the iframe has finished loading, tell it what to do
- GM_setValue(funcvar, encodeURI(data_func.toString()));
- command_hub.contentWindow.postMessage({ status: "run" }, "*");
- } else if(event.data.status == "finished") {
- // wait until the values have been set
- var temp_interval = setInterval(function() {
- if(typeof GM_getValue(retvar) !== "undefined") {
- clearInterval(temp_interval);
-
- // the iframe has finished, send the data to the callback and close the frame
- document.body.removeChild(command_hub);
- callback(GM_getValue(retvar));
- GM_deleteValue(retvar);
- }
- }, 100);
- }
- }
- });
- },
-
- /**
- *
- */
- init: function() {
- var hostname = document.location.hostname.replace(/\./g, "_"),
- funcvar = `command_hub_func_${hostname}`,
- retvar = `command_hub_return_${hostname}`;
-
- // wait for our parent to tell us what to do
- window.addEventListener("message", function(event) {
- if(event.source == parent) {
- if(event.data.status == "run") {
- // wait until the values have been set
- var temp_interval = setInterval(function() {
- if(typeof GM_getValue(funcvar) !== "undefined") {
- clearInterval(temp_interval);
- GM_setValue(retvar, eval(`(${decodeURI(GM_getValue(funcvar))})`)());
- GM_deleteValue(funcvar);
- parent.postMessage({ status: "finished" }, "*");
- }
- }, 100);
- }
- }
- });
-
- // let the parent know the iframe is ready
- parent.postMessage({status: "ready"}, "*");
- }
- };
- })();
-
- if(document.location.hostname == "gleam.io") {
- giveawayHelperUI.loadUI();
- gleamHelper.init();
- } else if(document.location.hostname == "marvelousga.com") {
- giveawayHelperUI.loadUI();
- marvelousHelper.init();
- } else if(document.location.hostname == "simplo.gg") {
- giveawayHelperUI.loadUI();
- simploHelper.init();
- } else {
- commandHub.init();
- }
-})();