Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. // ==UserScript==
  2. // @name GBF Raidfinder | Viramate Web API Integration
  3. // @namespace http://fabulous.cupcake.jp.net
  4. // @version 0.6.0
  5. // @description Join raids faster by utilizing Viramate Web API
  6. // @author FabulousCupcake
  7. // @include /^https?:\/\/.+-raidfinder\.herokuapp\.com.*$/
  8. // @include /^https?:\/\/gbf-raidfinder\.aikats\.us.*$/
  9. // @include /^https?:\/\/gbf-raidfinder\.la-foret\.me.*$/
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. const DEBUG = false;
  15. const API_URL = "chrome-extension://fgpokpknehglcioijejfeebigdnbnokj/content/api.html";
  16. let API_HOST;
  17. let pendingRequests = {};
  18. let messageId = 1;
  19.  
  20.  
  21. // Utilities
  22. function log(...args) {
  23. if (!DEBUG) return;
  24. console.debug("vm_webapi_integration »", ...args);
  25. }
  26.  
  27. function showSnackbar(message, timeout=1000) {
  28. const snackbarEl = document.querySelector(".mdl-snackbar");
  29. const data = { message, timeout };
  30.  
  31. snackbarEl.MaterialSnackbar.showSnackbar(data);
  32. }
  33.  
  34. function waitForElementToExist(selector, action) {
  35. const elm = document.querySelector(selector);
  36.  
  37. if (elm !== null) return action(elm);
  38. setTimeout(waitForElementToExist.bind(null, selector, action), 1000);
  39. }
  40.  
  41. // API Stuff
  42. function sendApiRequest(request, callback) {
  43. const message = {
  44. ...request,
  45. id: messageId,
  46. };
  47.  
  48. log(`Sending request #${messageId}`);
  49. pendingRequests[messageId] = callback;
  50. API_HOST.contentWindow.postMessage(message, "*");
  51. messageId += 1
  52. }
  53.  
  54. function tryJoinRaid(raidCode, cb) {
  55. const message = {
  56. type: "tryJoinRaid",
  57. raidCode
  58. };
  59.  
  60. sendApiRequest(message, (result) => {
  61. if (result) showSnackbar(result.replace("popup: ", ""));
  62. if (typeof cb === "function") cb(result);
  63. });
  64. }
  65.  
  66. function tryGetVersion() {
  67. const message = {
  68. type: "getVersion"
  69. };
  70.  
  71. sendApiRequest(message, (result) => {
  72. if (result) {
  73. log("Viramate API loaded")
  74. hookExistingRaids();
  75. observeExistingColumns();
  76. observeNewColumns();
  77. showSnackbar(`Viramate ${result} API Loaded!`, 3000);
  78. } else {
  79. showSnackbar(`Viramate Web API is disabled. Please enable it in Viramate settings page!`, 5000);
  80. }
  81. });
  82. }
  83.  
  84. function onApiLoaded() {
  85. log("Viramate iframe loaded");
  86. window.addEventListener("message", onApiMessage);
  87. tryGetVersion();
  88. }
  89.  
  90. function onApiMessage(evt) {
  91. if (evt.data.type !== "result") return;
  92.  
  93. if (evt.data.result && evt.data.result.error) {
  94. log("Request failed", evt.data.result.error);
  95. } else {
  96. log("Got request response", evt.data);
  97. }
  98.  
  99. const callback = pendingRequests[evt.data.id];
  100. if (!callback) return;
  101.  
  102. callback(evt.data.result);
  103. pendingRequests[evt.data.id] = null;
  104. }
  105.  
  106. function insertIframe() {
  107. log("Loading Viramate API…");
  108. API_HOST = document.createElement("iframe");
  109. API_HOST.id = "viramate_api_host";
  110. API_HOST.src = API_URL;
  111. API_HOST.style = "display: none;";
  112. API_HOST.addEventListener("load", onApiLoaded);
  113.  
  114. document.body.appendChild(API_HOST);
  115. }
  116.  
  117.  
  118. // DOM stuff
  119. // Dim all raid entry with the same code
  120. function dimRaids(raidCode) {
  121. const els = document.querySelectorAll(`li[data-raidid="${raidCode}"]`);
  122. els.forEach(el => {
  123. el.classList.add("gbfrf-tweet--copied");
  124. });
  125. }
  126.  
  127. // Adds click event listener to `el`
  128. function hookClickToJoin(el) {
  129. let raidCode = el.attributes["data-raidid"].value;
  130. el.addEventListener("click", (e) => {
  131. e.stopPropagation();
  132. tryJoinRaid(raidCode, (msg) => {
  133. switch(msg) {
  134. case "ok":
  135. case "popup: The number that you entered doesn't match any battle.":
  136. case "popup: This raid battle has already ended.":
  137. case "popup: This raid battle is full. You can't participate.":
  138. case "popup: This raid battle has already ended.":
  139. case "already in this raid":
  140. dimRaids(raidCode);
  141. break;
  142. default:
  143. return;
  144. }
  145. });
  146. });
  147. }
  148.  
  149. // Adds click event listener to all existing raids in the page
  150. function hookExistingRaids() {
  151. const raids = document.querySelectorAll(".gbfrf-tweet");
  152.  
  153. raids.forEach(raid => hookClickToJoin(raid));
  154. }
  155.  
  156. // Observes `el` for new raids and add click event listener to it
  157. function observeNewRaids(raidColumnEl) {
  158. const tweets = raidColumnEl.querySelector(".gbfrf-tweets");
  159. const config = { childList: true };
  160. const observer = new MutationObserver(mutations => {
  161. mutations.forEach(mutation => {
  162. mutation.addedNodes.forEach(node => {
  163. hookClickToJoin(node);
  164. });
  165. });
  166. });
  167.  
  168. log("Observing raids in column", raidColumnEl);
  169. observer.observe(tweets, config);
  170. }
  171.  
  172. // Adds new raid observer to existing columns
  173. function observeExistingColumns() {
  174. const columns = document.querySelectorAll(".gbfrf-column");
  175.  
  176. columns.forEach(column => observeNewRaids(column));
  177. }
  178.  
  179. // Adds new raid observer to new columns
  180. function observeNewColumns() {
  181. const columns = document.querySelector('.gbfrf-columns');
  182. const config = { childList: true };
  183. const observer = new MutationObserver( mutations => {
  184. mutations.forEach(mutation => {
  185. mutation.addedNodes.forEach(node => {
  186. log("New column found");
  187. observeNewRaids(node);
  188. });
  189. });
  190. });
  191.  
  192. log("Observing for new columns…");
  193. observer.observe(columns, config);
  194. }
  195.  
  196.  
  197. // Main
  198. function init() {
  199. log("Initialized");
  200. waitForElementToExist(".mdl-list.gbfrf-tweets", insertIframe);
  201. }
  202.  
  203. init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement