Fireblend / MafiEraVoteTool

// ==UserScript==
// @name        MafiEraVoteTool
// @version     4
// @namespace   http://www.fireblend.com/
// @updateURL   https://openuserjs.org/meta/Fireblend/MafiEraVoteTool.meta.js
// @downloadURL https://openuserjs.org/src/scripts/Fireblend/MafiEraVoteTool.user.js
// @license     MIT
// @icon        https://media.discordapp.net/attachments/165286383257255936/429860317649174529/imageedit__4571108207.png
// @homepageURL http://www.fireblend.com/
// @author      Sergio Morales
// @description The MafiEra Vote Tool, made easier!
// @include     http*://www.resetera.com/threads/*
// @include     http*://www.resetera.com/threads/*/*
// @require     https://code.jquery.com/ui/1.12.1/jquery-ui.js
// @resource    customCSS https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css
// @run-at      document-idle
// @grant       GM_addStyle
// @grant       GM_getResourceText
// ==/UserScript==

(function () {
  'use strict';

  function addButton() {
    var buttonLocations1 = document.querySelectorAll('div.pageNav.pageNav--skipStart');
    var a1 = makeButton();
    buttonLocations1[0].appendChild(a1);
    var b1 = makeButton();
    buttonLocations1[1].appendChild(b1);
  }

  function openDialog() {
    var url = window.location.href;
    var page = "https://vote.fireblend.com/" + url.split("/threads/")[1].split("/")[0] + "/simple";

    var dlg_width = 500;
    var dlg_height = 600;

    var $dialog = $('<div></div>')
      .html('<iframe id="myDialog" style="background-color: #ffffff; border: 0px; " src="' + page + '" width="100%" height="100%"></iframe>')
      .dialog({
        autoOpen: false,
        modal: true,
        height: dlg_height,
        width: dlg_width,
        position: {my: "center center", at: "center center", of: window},
        title: "MafiEra Vote Count",
        resizable: false
      });

    $(window).bind('scroll', function(evt){
        $dialog.dialog("option", {"position": {my: "center center", at: "center center", of: window}});
    });

    $dialog.dialog('open');
  }

  function makeButton() {
    var a = document.createElement('a');
    var url = window.location.href;
    a.setAttribute('style', 'cursor:pointer; padding=30px;');
    a.addEventListener("click", openDialog, false);
    a.setAttribute('target', '_blank');
    a.style.marginLeft = "15px";
    a.style.alignSelf = "center";
    a.appendChild(document.createTextNode("Vote Count"));
    return a;
  }

  var newCSS = GM_getResourceText("customCSS");
  GM_addStyle(newCSS);
  GM_addStyle('.ui-dialog .ui-dialog-content { overflow: hidden; }');
  addButton();
})();