Advertisement
Degritone

BBS Master Script

Nov 14th, 2021 (edited)
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name             BBS Master Script
  3. // @namespace        dollars-bbs.org
  4. // @author           Degritone
  5. // @description      Allows Degritone's BBS scripts to auto-update
  6. // @match            *://*/*
  7. // @grant            GM.setValue
  8. // @grant            GM.getValue
  9. // @grant            GM.deleteValue
  10. // ==/UserScript==
  11.  
  12. let version = 1.02;
  13.  
  14. function request(url){
  15.   let xmlHttp = new XMLHttpRequest();
  16.   xmlHttp.open("GET",url,false);
  17.   xmlHttp.send(null);
  18.   return xmlHttp.responseText;
  19. }
  20.  
  21. let scriptToggles = [];
  22.  
  23. function settings(scriptNames,scriptDescriptions,runnable){
  24.   if(scriptNames.length<1){
  25.     setTimeout(run,1000);
  26.     return;
  27.   }
  28.   if(window.location.href.startsWith("https://dollars-bbs.org")){
  29.     settingHeader();
  30.   }
  31.   if(window.location.href == "https://dollars-bbs.org/settings"){
  32.     let div = document.createElement("html");
  33.     div.innerHTML = request("https://dollars-bbs.org/updates.html");
  34.     document.getElementsByTagName("html")[0].innerHTML = div.innerHTML;
  35.     let container = document.getElementById("contentarea");
  36.     if(!container){
  37.       settings(scriptNames,scriptDescriptions,runnable);
  38.       return;
  39.     }
  40.     container.innerHTML = "<div>Here, you can select which of Name's/Degritone's Scripts to activate.</div><br><br>";
  41.     let toggles = [];
  42.     for(let i=0;i<scriptNames.length;i++){
  43.       let label = document.createElement("label");
  44.       let checkbox = document.createElement("input");
  45.       toggles[i] = document.createElement("div");
  46.       toggles[i].appendChild(checkbox);
  47.       toggles[i].appendChild(label);
  48.       container.appendChild(toggles[i]);
  49.  
  50.       checkbox.type = "checkbox";
  51.       checkbox.id = scriptNames[i];
  52.       checkbox.checked = scriptToggles[i];
  53.       label.htmlFor = scriptNames[i];
  54.       label.innerHTML = scriptNames[i]+": "+scriptDescriptions[i];
  55.     }
  56.     let d = document.createElement("button");
  57.     container.appendChild(d);
  58.     d.innerHTML = "Apply";
  59.     d.onclick = ()=>saveMCookie(toggles,runnable);
  60.     d.style = "position:relative; margin:auto; border-radius:7px; border:solid 2px #EEEEEE; background-color:#151515; padding:5px; padding-top:3px; text-decoration:none; font-size:14px; cursor:pointer; color:#EEEEEE;";
  61.  
  62.     container.style.width = "calc(100% - 180px)";
  63.     container.appendChild(document.createElement("br"));
  64.     container.appendChild(document.createElement("br"));
  65.  
  66.     document.title = "Dollars BBS | Settings";
  67.   }
  68. }
  69.  
  70. function settingHeader(){
  71.   let pmenu = document.getElementById("pagemenu");
  72.   if(!pmenu){
  73.     setTimeout(settingHeader,1000);
  74.     return;
  75.   }
  76.   let li = document.createElement("li");
  77.   li.innerHTML = "<a href='https://dollars-bbs.org/settings'>Settings</a>";
  78.   pmenu.appendChild(li);
  79. }
  80.  
  81. function saveMCookie(toggles,runnable){
  82.   for(let i=0;i<toggles.length;i++){
  83.     for(let j=0;j<runnable[i].length;j++){
  84.       GM.setValue("bbsMasterSettings."+toggles[i].children[0].id.replace(" ",""),toggles[i].children[0].checked);
  85.     }
  86.   }
  87.   window.location.reload();
  88. }
  89.  
  90. async function getMCookie(scriptNames){
  91.   for(let i=0;i<scriptNames.length;i++){
  92.     scriptToggles[i] = await GM.getValue("bbsMasterSettings."+scriptNames[i].replace(" ",""),false);
  93.   }
  94. }
  95.  
  96. async function run(){
  97.   let runnable = [];
  98.   let scriptURLs = [];
  99.   let scriptNames = [];
  100.   let scriptDescriptions = [];
  101.   let masterURL = "https://pastebin.com/raw/y2wJrPKb";
  102.   let aoonline = getAOOnline();
  103.   let scriptContainer = await getScript(aoonline,"container",masterURL);
  104.   if(!scriptContainer)
  105.     return;
  106.   eval(scriptContainer);
  107.   await getMCookie(scriptNames);
  108.   settings(scriptNames,scriptDescriptions,runnable);
  109.   for(let i=0;i<scriptToggles.length;i++){
  110.     if(!scriptToggles[i])
  111.       continue;
  112.     let c = false;
  113.     for(let j=0;j<runnable[i].length;j++){
  114.       c = c || window.location.href.startsWith(runnable[i][j]);
  115.     }
  116.     if(!c)
  117.       continue;
  118.     let script = await getScript(aoonline,scriptNames[i].replace(" ",""),scriptURLs[i]);
  119.     if(!script)
  120.       continue;
  121.     runScript(script);
  122.   }
  123. }
  124.  
  125. async function runScript(script){
  126.   eval(script);
  127. }
  128.  
  129. function getAOOnline(){
  130.   let xmlHttp = new XMLHttpRequest();
  131.   xmlHttp.open("GET","https://api.allorigins.win/raw?url=https://pastebin.com/",false);
  132.   try{
  133.     xmlHttp.send(null);
  134.   }catch(e){
  135.     return false;
  136.   }
  137.   return xmlHttp.status==200;
  138. }
  139.  
  140. async function getScript(aoonline,name,URL){
  141.   let allorigins = "https://api.allorigins.win/raw?cacheMaxAge=0&disableCache=true&url=";
  142.   if(aoonline){
  143.     let script;
  144.     try{
  145.       script = request(allorigins+URL);
  146.       if(script.match(/<!DOCTYPE html>/))
  147.         throw {message:"request error"};
  148.     }catch(e){
  149.       console.log("allorigins online, failed to load script for unknown reasons.");
  150.       console.log("Attempting to load script from local backup.");
  151.       console.log("Error: "+e.message);
  152.       return await GM.getValue("bbsMasterSettings.script."+name,null);
  153.     }
  154.     GM.setValue("bbsMasterSettings.script."+name,script);
  155.     return script;
  156.   }else
  157.     return await GM.getValue("bbsMasterSettings.script."+name,null);
  158. }
  159.  
  160. run();
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement