Kong Flash API Patcher

Intercepts Kongregate Flash API fetch() request to return a modified version that will function properly

  1. // ==UserScript==
  2. // @name Kong Flash API Patcher
  3. // @version 1.2
  4. // @author Colin969
  5. // @description Intercepts Kongregate Flash API fetch() request to return a modified version that will function properly
  6. // @license MIT
  7. // @match *://*.konggames.com/games/*/*/frame/*
  8. // @match *://www.kongregate.com/games/*/*
  9. // @match *://www.kongregate.com/*/games/*/*
  10. // @match *://www.kongregate.com/accounts/*/card_album*
  11. // @run-at document-start
  12. // @grant none
  13. // @namespace https://greasyfork.org/users/1296974
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. // Create JS replacement funcs
  19. window.flashApiSendMessage = (opcode, parameters) => {
  20. setTimeout(() => {
  21. kongregateAPI.messageConnection.sendMessage(opcode, decodeURIComponent(parameters));
  22. });
  23. };
  24.  
  25. window.flashApiBootstrap = (swfId, apiUrl) => {
  26. console.log('Flash setting up from ' + swfId);
  27. setTimeout(() => {
  28. function setGameSwf(){
  29. if (typeof kongregateAPI !== 'undefined') {
  30. // Ruffle doesn't have Play function, removed from check
  31. kongregateAPI._isSwf = function(e) {
  32. return e && void 0 !== e.setConnectionObject;
  33. };
  34.  
  35. // Modified to check for ruffle-player, not just objects and embeds
  36. kongregateAPI._findSwf = function(e) {
  37. var t = document.getElementById(e),
  38. n = this,
  39. s = function(e) {
  40. for (var s = 0; s < e.length; s++)
  41. if (t = e[s], n._isSwf(t)) return t
  42. };
  43. return this._isSwf(t) ? t : t = s(document.getElementsByName(e)) || s(document.querySelectorAll("[id='" + e + "']")) || s(document.getElementsByTagName("object")) || s(document.getElementsByTagName("embed")) || s(document.getElementsByTagName("ruffle-player"))
  44. };
  45. setTimeout(function(){ kongregateAPI._setGameSwf(swfId); }, 1);
  46. return true;
  47. }
  48. }
  49.  
  50. function loadKongregateApi() {
  51. var s = document.createElement('script');
  52. s.type ='text/javascript';
  53. s.src = apiUrl;
  54. if(s.addEventListener) {
  55. s.addEventListener('load', setGameSwf)
  56. } else if(s.readyState) {
  57. s.onreadystatechange = setGameSwf;
  58. }
  59. document.getElementsByTagName('head')[0].appendChild(s);
  60. }
  61.  
  62. if (!setGameSwf()) {
  63. loadKongregateApi();
  64. }
  65. return 'pending';
  66. });
  67. return "pending";
  68. };
  69.  
  70. // Replace fetch so we can capture the api
  71. const originalFetch = window.fetch;
  72.  
  73. window.fetch = function(req, options) {
  74. try {
  75. let request = new URL(req instanceof Request ? req.url : req);
  76.  
  77. if (request.href.includes("API_AS3_d43c4b859e74432475c1627346078677.swf") || request.href.includes("/flash/API_AS3.swf")) {
  78. request.href = "https://colin969.github.io/Kongregate-Patched-APIs/API_AS3_MODIFIED.swf";
  79. }
  80.  
  81. if (request.href.includes("API_f99fa1a5a43e48224ae2c0177064456d.swf") || request.href.includes("/flash/API.swf")) {
  82. request.href = "https://colin969.github.io/Kongregate-Patched-APIs/API_AS2_MODIFIED.swf";
  83. }
  84. return originalFetch(request, options);
  85. } catch {
  86. // Ignore, just pass through
  87. return originalFetch(req, options);
  88. }
  89. };
  90. })();