Skip to content

Instantly share code, notes, and snippets.

@AbrahamAriel
Last active April 1, 2023 04:49
Show Gist options
  • Save AbrahamAriel/7a8d7cea1d8cbcd82700d67a09942a47 to your computer and use it in GitHub Desktop.
Save AbrahamAriel/7a8d7cea1d8cbcd82700d67a09942a47 to your computer and use it in GitHub Desktop.
/**
* ChoiceScriptSavePluginInjector
*
* ---
*
* Licensed under the MIT License.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* ---
*
* ChoiceScriptSavePlugin by CJW (ChoiceScriptIDE):
* https://github.com/ChoicescriptIDE/ChoiceScriptSavePlugin,
* https://forum.choiceofgames.com/t/choicescript-saving-plugin-update-sept-2019/983
*
*/
// ==UserScript==
// @name ChoiceScriptSavePluginInjector
// @author AbrahamAriel
// @description Injects ChoicescriptIDE/ChoiceScriptSavePlugin into any CoG games.
// @version 1.8.1
// @license MIT
// @namespace https://gist.github.com/AbrahamAriel/7a8d7cea1d8cbcd82700d67a09942a47
// @downloadURL https://gist.githubusercontent.com/AbrahamAriel/7a8d7cea1d8cbcd82700d67a09942a47/raw/ChoiceScriptSavePluginInjector.user.js
// @updateURL https://gist.githubusercontent.com/AbrahamAriel/7a8d7cea1d8cbcd82700d67a09942a47/raw/ChoiceScriptSavePluginInjector.user.js
// @homepage https://www.reddit.com/r/choiceofgames/comments/ovo3eh/choicescriptsaveplugininjector_add_save_system_to/
// @match http://choiceofgames.com/*
// @match https://choiceofgames.com/*
// @match http://www.choiceofgames.com/*
// @match https://www.choiceofgames.com/*
// @match http://choiceofgames.com/user-contributed/*
// @match https://choiceofgames.com/user-contributed/*
// @match http://www.choiceofgames.com/user-contributed/*
// @match https://www.choiceofgames.com/user-contributed/*
// @exclude http://www.choiceofgames.com/category/*
// @exclude https://www.choiceofgames.com/category/*
// @exclude http://www.choiceofgames.com/profile/*
// @exclude https://www.choiceofgames.com/profile/*
// @exclude http://www.choiceofgames.com/blog/*
// @exclude https://www.choiceofgames.com/blog/*
// @exclude http://www.choiceofgames.com/api/*
// @exclude https://www.choiceofgames.com/api/*
// @exclude http://www.choiceofgames.com/about-us/*
// @exclude https://www.choiceofgames.com/about-us/*
// @exclude http://www.choiceofgames.com/contact-us/*
// @exclude https://www.choiceofgames.com/contact-us/*
// @exclude http://www.choiceofgames.com/privacy-policy/*
// @exclude https://www.choiceofgames.com/privacy-policy/*
// @exclude http://www.choiceofgames.com/looking-for-writers/*
// @exclude https://www.choiceofgames.com/looking-for-writers/*
// @exclude http://www.choiceofgames.com/make-your-own-games/*
// @exclude https://www.choiceofgames.com/make-your-own-games/*
// @exclude http://choiceofgames.com/category/*
// @exclude https://choiceofgames.com/category/*
// @exclude http://choiceofgames.com/profile/*
// @exclude https://choiceofgames.com/profile/*
// @exclude http://choiceofgames.com/blog/*
// @exclude https://choiceofgames.com/blog/*
// @exclude http://choiceofgames.com/api/*
// @exclude https://choiceofgames.com/api/*
// @exclude http://choiceofgames.com/about-us/*
// @exclude https://choiceofgames.com/about-us/*
// @exclude http://choiceofgames.com/contact-us/*
// @exclude https://choiceofgames.com/contact-us/*
// @exclude http://choiceofgames.com/privacy-policy/*
// @exclude https://choiceofgames.com/privacy-policy/*
// @exclude http://choiceofgames.com/looking-for-writers/*
// @exclude https://choiceofgames.com/looking-for-writers/*
// @exclude http://choiceofgames.com/make-your-own-games/*
// @exclude https://choiceofgames.com/make-your-own-games/*
// @run-at document-start
// @grant GM_getResourceText
// @resource ChoiceScriptSavePlugin https://cdn.jsdelivr.net/gh/AbrahamAriel/ChoiceScriptSavePlugin@latest/ChoiceScriptSavePlugin.js
// ==/UserScript==
(function() {
'use strict';
const savePluginElement = Object.assign(document.createElement('script'), {
type: 'text/javascript',
innerHTML: GM_getResourceText('ChoiceScriptSavePlugin')
});
const navigatorScript = /(navigator\.js)/;
const mygame = /(mygame\.js)/;
const navigatorObserver = new MutationObserver(function (records) {
records.forEach(function (mutation) {
Array.prototype.forEach.call(mutation.addedNodes, function (node) {
if ("SCRIPT" === node.tagName) {
if (navigatorScript.test(node.src)) {
try {
node.after(savePluginElement);
navigatorObserver.disconnect();
} catch (error) {
console.error('Failed to attach quick save menu!', error);
}
}
}
});
});
});
navigatorObserver.observe(document.head, {
subtree: true,
childList: true,
attributes: true,
attributesFilter: ['src'],
characterData: false,
});
document.addEventListener("DOMContentLoaded", (event) => {
/** Stops MutationObserver after 5 seconds
* To prevent it from hogging unnecessary
* resources.
*/
setTimeout(() => {
console.log('disconnect');
navigatorObserver.disconnect();
}, 5000);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment