MSPFA extras

Adds custom quality of life features to MSPFA.

当前为 2020-08-23 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MSPFA extras
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.6.6.1
  5. // @description Adds custom quality of life features to MSPFA.
  6. // @author seymour schlong
  7. // @icon https://pipe.miroware.io/5b52ba1d94357d5d623f74aa/mspfa/ico.png
  8. // @icon64 https://pipe.miroware.io/5b52ba1d94357d5d623f74aa/mspfa/ico.png
  9. // @match https://mspfa.com/
  10. // @match https://mspfa.com/*/
  11. // @match https://mspfa.com/*/?*
  12. // @match https://mspfa.com/?s=*
  13. // @match https://mspfa.com/my/*
  14. // @match https://mspfa.com/random/
  15. // @exclude https://mspfa.com/js/*
  16. // @exclude https://mspfa.com/css/*
  17. // @grant none
  18. // ==/UserScript==
  19.  
  20.  
  21. (function() {
  22. 'use strict';
  23.  
  24. const currentVersion = "1.6.6.1";
  25. console.log(`MSPFA extras script v${currentVersion} by seymour schlong`);
  26.  
  27. const debug = false;
  28.  
  29. /**
  30. * https://github.com/GrantGryczan/MSPFA/projects/1?fullscreen=true
  31. * Github to-do completion list (and other stuff too)
  32. *
  33. * https://github.com/GrantGryczan/MSPFA/issues/26 - Dropdown menu - February 23rd, 2020
  34. * https://github.com/GrantGryczan/MSPFA/issues/18 - MSPFA themes - February 23rd, 2020
  35. * https://github.com/GrantGryczan/MSPFA/issues/32 - Adventure creation dates - February 23rd, 2020
  36. * https://github.com/GrantGryczan/MSPFA/issues/32 - User creation dates - February 23rd, 2020
  37. * https://github.com/GrantGryczan/MSPFA/issues/40 - Turn certain buttons into links - July 21st, 2020
  38. * https://github.com/GrantGryczan/MSPFA/issues/41 - Word and character count - July 21st, 2020
  39. * https://github.com/GrantGryczan/MSPFA/issues/57 - Default spoiler values - August 7th, 2020
  40. * https://github.com/GrantGryczan/MSPFA/issues/62 - Buttonless spoilers - August 7th, 2020
  41. * https://github.com/GrantGryczan/MSPFA/issues/52 - Hash URLs - August 8th, 2020
  42. * - Page drafts - August 8th, 2020
  43. * - Edit pages button - August 8th, 2020
  44. * - Image preloading - August 20th, 2020
  45. * https://github.com/GrantGryczan/MSPFA/issues/19 - Manage game saves - August 22nd, 2020
  46. *
  47. * Extension to-do... maybe...
  48. *
  49. * If trying to save a page and any other save button is not disabled, ask the user if they would rather Save All instead, or prompt to disable update notifications.
  50. */
  51.  
  52. // A general function that allows for waiting until a certain element appears on the page.
  53. const pageLoad = (fn, length) => {
  54. const interval = setInterval(() => {
  55. if (fn()) clearInterval(interval);
  56. }, length ? length*1000 : 500);
  57. };
  58.  
  59. // Saves the options data for the script.
  60. const saveData = (data) => {
  61. localStorage.mspfaextra = JSON.stringify(data);
  62. if (debug) {
  63. console.log('Settings:');
  64. console.log(data);
  65. }
  66. };
  67.  
  68. // Saves the data for drafts
  69. const saveDrafts = (data) => {
  70. localStorage.mspfadrafts = JSON.stringify(data);
  71. if (debug) {
  72. console.log('Drafts:');
  73. console.log(data);
  74. }
  75. };
  76.  
  77. // Encases an element within a link
  78. const addLink = (elm, url) => {
  79. const link = document.createElement('a');
  80. link.href = url;
  81. elm.parentNode.insertBefore(link, elm);
  82. link.appendChild(elm);
  83. };
  84.  
  85. // Returns true if version 2 is newer
  86. const compareVer = (ver1, ver2) => {
  87. ver1 = ver1.split(/\./); // current version
  88. ver2 = ver2.split(/\./); // new version
  89. ver1.push(0);
  90. ver1.push(0);
  91. ver2.push(0);
  92. ver2.push(0);
  93. if (parseInt(ver2[0]) > parseInt(ver1[0])) { // 1.x.x.x
  94. return true;
  95. } else if (parseInt(ver2[1]) > parseInt(ver1[1])) { // x.1.x.x
  96. return true;
  97. } else if (parseInt(ver2[2]) > parseInt(ver1[2])) { // x.x.1.x
  98. return true;
  99. } else if (parseInt(ver2[3]) > parseInt(ver1[3]) && parseInt(ver2[2]) === parseInt(ver1[2])) { // x.x.x.1
  100. return true;
  101. }
  102. return false;
  103. }
  104.  
  105. // Easy br element
  106. const newBr = () => {
  107. return document.createElement('br');
  108. }
  109.  
  110. let settings = {};
  111. let drafts = {};
  112.  
  113. const defaultSettings = {
  114. autospoiler: false,
  115. style: 0,
  116. styleURL: "",
  117. night: false,
  118. auto502: true,
  119. textFix: false,
  120. pixelFix: false,
  121. intro: false,
  122. autoUpdate: true,
  123. commandScroll: false,
  124. preload: true,
  125. version: currentVersion,
  126. spoilerValues: {}
  127. }
  128.  
  129. let pageLoaded = false;
  130.  
  131. if (localStorage.mspfadrafts) {
  132. Object.assign(drafts, JSON.parse(localStorage.mspfadrafts));
  133. }
  134.  
  135. // Load any previous settings from localStorage
  136. if (localStorage.mspfaextra) {
  137. Object.assign(settings, JSON.parse(localStorage.mspfaextra));
  138.  
  139. // Get draft data from settings
  140. if (typeof settings.drafts === "object") {
  141. if (Object.keys(settings.drafts).length > 0 && Object.keys(drafts).length === 0) {
  142. drafts = settings.drafts;
  143. }
  144. }
  145. saveDrafts(drafts);
  146. }
  147.  
  148. // If any settings are undefined, re-set to their default state. (For older users when new things get stored)
  149. const checkSettings = () => {
  150. const defaultSettingsKeys = Object.keys(defaultSettings);
  151. for (let i = 0; i < defaultSettingsKeys.length; i++) {
  152. if (typeof settings[defaultSettingsKeys[i]] === "undefined") {
  153. settings[defaultSettingsKeys[i]] = defaultSettings[defaultSettingsKeys[i]];
  154. }
  155. }
  156. saveData(settings);
  157. }
  158.  
  159. checkSettings();
  160.  
  161. // Update saved version to the version used in the script to prevent unnecessary notifications
  162. if (compareVer(settings.version, currentVersion)) {
  163. settings.version = currentVersion;
  164. saveData(settings);
  165. }
  166.  
  167. // Scrolls you to where you need to be
  168. const hashSearch = location.href.replace(location.origin + location.pathname, '').replace(location.search, '');
  169. if (hashSearch !== '') {
  170. pageLoad(() => {
  171. const idElement = document.querySelector(hashSearch);
  172. if (idElement) {
  173. const selected = document.querySelector(hashSearch);
  174. selected.scrollIntoView();
  175. selected.style.boxShadow = '1px 1px 5px red, -1px -1px 5px red, -1px 1px 5px red, -1px 1px 5px red';
  176. selected.style.transition = '0.5s';
  177. pageLoad(() => {
  178. if (pageLoaded) {
  179. selected.style.boxShadow = '';
  180. }
  181. });
  182.  
  183. return true;
  184. }
  185. }, 1);
  186. }
  187.  
  188. // Ripped shamelessly right from mspfa lol (URL search parameters -- story ID, page num, etc.)
  189. let rawParams;
  190. if (location.href.indexOf("#") != -1) {
  191. rawParams = location.href.slice(0, location.href.indexOf("#"));
  192. } else {
  193. rawParams = location.href;
  194. }
  195. if (rawParams.indexOf("?") != -1) {
  196. rawParams = rawParams.slice(rawParams.indexOf("?") + 1).split("&");
  197. } else {
  198. rawParams = [];
  199. }
  200. const params = {};
  201. for (let i = 0; i < rawParams.length; i++) {
  202. try {
  203. const p = rawParams[i].split("=");
  204. params[p[0]] = decodeURIComponent(p[1]);
  205. } catch (err) {}
  206. }
  207.  
  208. if (debug) {
  209. console.log('URL parameters:');
  210. console.log(params);
  211. }
  212.  
  213. // Functions to get/change data from the console
  214. window.MSPFAe = {
  215. getSettings: () => {
  216. return settings;
  217. },
  218. getSettingsString: (formatted) => {
  219. if (formatted) {
  220. console.log(JSON.stringify(settings, null, 4));
  221. } else {
  222. console.log(JSON.stringify(settings));
  223. }
  224. },
  225. getDrafts: () => {
  226. return drafts;
  227. },
  228. getDraftsString: (formatted) => {
  229. if (formatted) {
  230. console.log(JSON.stringify(drafts, null, 4));
  231. } else {
  232. console.log(JSON.stringify(drafts));
  233. }
  234. },
  235. changeSettings: (newSettings) => {
  236. console.log('Settings updated');
  237. console.log(settings);
  238. Object.assign(settings, newSettings);
  239. saveData(settings);
  240. },
  241. changeSettingsString: (fullString) => {
  242. try {
  243. JSON.parse(fullString);
  244. } catch (err) {
  245. console.error(err);
  246. return;
  247. }
  248. settings = JSON.parse(fullString);
  249. checkSettings();
  250. console.log(settings);
  251. },
  252. getParams: params
  253. }
  254.  
  255. // Delete any unchanged spoiler values or empty drafts
  256. if (location.pathname !== "/my/stories/pages/") {
  257. // Go through spoiler values and remove any that aren't unique
  258. Object.keys(settings.spoilerValues).forEach(adventure => {
  259. if (settings.spoilerValues[adventure].open === "Show" && settings.spoilerValues[adventure].close === "Hide") {
  260. delete settings.spoilerValues[adventure];
  261. } else if (settings.spoilerValues[adventure].open === '' && settings.spoilerValues[adventure].close === '') {
  262. delete settings.spoilerValues[adventure];
  263. }
  264. });
  265. // Go through and remove adventures with empty drafts
  266. Object.keys(drafts).forEach(adventure => {
  267. if (Object.keys(drafts[adventure]).length === 0) {
  268. delete drafts[adventure];
  269. }
  270. });
  271. }
  272.  
  273. const styleOptions = ["Standard", "Low Contrast", "Light", "Dark", "Felt", "Trickster", "Custom"];
  274. const styleUrls = ['', '/css/theme1.css', '/css/theme2.css', '/css/?s=36237', '/css/theme4.css', '/css/theme5.css'];
  275.  
  276. // Dropdown menu
  277. const myLink = document.querySelector('nav a[href="/my/"]');
  278. if (myLink) {
  279. const dropDiv = document.createElement('div');
  280. dropDiv.className = 'dropdown';
  281. Object.assign(dropDiv.style, {
  282. position: 'relative',
  283. display: 'inline-block',
  284. backgroundColor: 'inherit'
  285. });
  286.  
  287. const dropContent = document.createElement('div');
  288. dropContent.className = 'dropdown-content';
  289. Object.assign(dropContent.style, {
  290. display: 'none',
  291. backgroundColor: 'inherit',
  292. position: 'absolute',
  293. textAlign: 'left',
  294. minWidth: '100px',
  295. marginLeft: '-5px',
  296. padding: '2px',
  297. zIndex: '1',
  298. borderRadius: '0 0 5px 5px'
  299. });
  300.  
  301. dropDiv.addEventListener('mouseenter', evt => {
  302. dropContent.style.display = 'block';
  303. dropContent.style.color = getComputedStyle(myLink).color;
  304. });
  305. dropDiv.addEventListener('mouseleave', evt => {
  306. dropContent.style.display = 'none';
  307. });
  308.  
  309. myLink.parentNode.insertBefore(dropDiv, myLink);
  310. dropDiv.appendChild(myLink);
  311. dropDiv.appendChild(dropContent);
  312.  
  313. const dLinks = [];
  314. dLinks[0] = [ 'Messages', 'My Adventures', 'Settings' ];
  315. dLinks[1] = [ '/my/messages/', '/my/stories/', '/my/settings/' ];
  316.  
  317. for (let i = 0; i < dLinks[0].length; i++) {
  318. const newLink = document.createElement('a');
  319. newLink.textContent = dLinks[0][i];
  320. newLink.href = dLinks[1][i];
  321. newLink.style = 'visibility: visible; word-spacing: normal; letter-spacing: normal; font-size: 10px;';
  322. dropContent.appendChild(newLink);
  323. }
  324.  
  325. // Append "My Profile" to the dropdown list if you're signed in
  326. pageLoad(() => {
  327. if (window.MSPFA) {
  328. if (window.MSPFA.me.n) {
  329. const newLink = document.createElement('a');
  330. newLink.textContent = "My Profile";
  331. newLink.href = `/user/?u=${window.MSPFA.me.i}`;
  332. newLink.style = 'visibility: visible; word-spacing: normal; letter-spacing: normal; font-size: 10px;';
  333. dropContent.appendChild(newLink);
  334. return true;
  335. }
  336. }
  337. });
  338. }
  339.  
  340. // Error reloading
  341. window.addEventListener("load", () => {
  342. pageLoaded = true;
  343.  
  344. // Reload the page if 502 CloudFlare error page appears
  345. if (settings.auto502 && document.querySelector('.cf-error-overview')) {
  346. window.location.reload();
  347. }
  348.  
  349. // Wait five seconds, then refresh the page
  350. if (document.body.textContent === "Your client is sending data to MSPFA too quickly. Wait a moment before continuing.") {
  351. setTimeout(() => {
  352. window.location.reload();
  353. }, 5000);
  354. }
  355. });
  356.  
  357. // Message that shows when you first get the script
  358. const showIntroDialog = () => {
  359. const msg = window.MSPFA.parseBBCode('Hi! Thanks for installing this script!\n\nBe sure to check the [url=https://greasyfork.org/en/scripts/396798-mspfa-extras#additional-info]GreasyFork[/url] page to see a full list of features, and don\'t forget to check out your [url=https://mspfa.com/my/settings/#extraSettings]settings[/url] page to tweak things to how you want.\n\nIf you have any suggestions, or you find a bug, please be sure to let me know on Discord at [url=discord://discordapp.com/users/277928549866799125]@seymour schlong#3669[/url].\n\n[size=12]This dialog will only appear once. To view it again, click "View Script Message" at the bottom of the site.[/size]');
  360. window.MSPFA.dialog("MSPFA extras message", msg, ["Okay"]);
  361. }
  362.  
  363. // Check for updates by comparing currentVersion to text data from a text file update text and info
  364. const checkForUpdates = (evt) => {
  365. const rand = Math.floor(Math.random() * 9999999999999); // Sometimes it doesn't fetch the right version of the text file.
  366. fetch(`https://pipe.miroware.io/5b52ba1d94357d5d623f74aa/mspfa/update.txt?rand=${rand}`, { cache:'no-store' }).then((req) => {
  367. if (req.ok) {
  368. return req.text().then(r => {
  369. const version = /^{(.*?)}/.exec(r)[1];
  370. const content = r.replace(/^{(.*?)}\r\n/, '');
  371.  
  372. if (compareVer(settings.version, version) || (evt && evt.type === 'click')) {
  373. const msg = window.MSPFA.parseBBCode(content);
  374. settings.version = version;
  375. saveData(settings);
  376. window.MSPFA.dialog(`MSPFA extras update! (${version})`, msg, ["Opt-out", "Dismiss", "Update"], (output, form) => {
  377. if (output === "Update") {
  378. window.open('https://greasyfork.org/en/scripts/396798-mspfa-extras', '_blank').focus();
  379. } else if (output === "Opt-out") {
  380. settings.autoUpdate = false;
  381. saveData(settings);
  382. }
  383. });
  384. } else {
  385. console.log('No new update found.');
  386. }
  387. });
  388. }
  389. });
  390. };
  391.  
  392. // Check for updates and show intro dialog if needed
  393. pageLoad(() => {
  394. if (window.MSPFA) {
  395. if (settings.autoUpdate) {
  396. console.log('Checking for updates...');
  397. checkForUpdates();
  398. }
  399.  
  400. if (!settings.intro) {
  401. showIntroDialog();
  402. settings.intro = true;
  403. saveData(settings);
  404. }
  405. return true;
  406. }
  407. });
  408.  
  409. const details = document.querySelector('#details');
  410.  
  411. // Add 'link' at the bottom to show the intro dialog again
  412. const introLink = document.createElement('a');
  413. introLink.textContent = 'View Script Message';
  414. introLink.href = 'javascript:void(0);';
  415. introLink.addEventListener('click', showIntroDialog);
  416. details.appendChild(introLink);
  417.  
  418. // vbar!!!!
  419. const vbar = document.createElement('span');
  420. Object.assign(vbar, {className: 'vbar', style: 'padding: 0 5px', textContent: '|'});
  421. details.appendChild(vbar);
  422.  
  423. // Add 'link' at the bottom to show the update dialog again
  424. const updateLink = document.createElement('a');
  425. updateLink.textContent = 'View Update';
  426. updateLink.href = 'javascript:void(0);';
  427. updateLink.addEventListener('click', checkForUpdates);
  428. details.appendChild(updateLink);
  429.  
  430. // vbar 2!!!!
  431. const vbar2 = document.createElement('span');
  432. Object.assign(vbar2, {className: 'vbar', style: 'padding: 0 5px', textContent: '|'});
  433. details.appendChild(vbar2);
  434.  
  435. // if you really enjoy the script and has some extra moneys 🥺
  436. const donateLink = document.createElement('a');
  437. donateLink.textContent = 'Donate';
  438. donateLink.href = 'https://ko-fi.com/ironbean';
  439. donateLink.target = "blank";
  440. details.appendChild(donateLink);
  441.  
  442. // Theme stuff
  443. const theme = document.createElement('link');
  444. Object.assign(theme, { id: 'theme', type: 'text/css', rel: 'stylesheet' });
  445. const updateTheme = (src) => {
  446. theme.href = src;
  447. }
  448. if (!document.querySelector('#theme')) {
  449. document.querySelector('head').appendChild(theme);
  450. if (settings.night) {
  451. updateTheme('/css/?s=36237');
  452. } else {
  453. updateTheme(settings.style == styleOptions.length - 1 ? settings.styleURL : styleUrls[settings.style]);
  454. }
  455. }
  456.  
  457. // Dropdown menu and pixelated scaling
  458. const dropStyle = document.createElement('style');
  459. const pixelFixText = 'img, .mspfalogo, .major, .arrow, #flashytitle, .heart, .fav, .notify, .edit, .rss, input, #loading { image-rendering: pixelated !important; }';
  460. const dropStyleText = `#notification { z-index: 2; } .dropdown-content a { color: inherit; padding: 2px; text-decoration: underline; display: block;}`;
  461. if (!document.querySelector('#dropdown-style')) {
  462. dropStyle.id = 'dropdown-style';
  463. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '');
  464. document.querySelector('head').appendChild(dropStyle);
  465. }
  466.  
  467. // Enabling night mode.
  468. document.querySelector('footer .mspfalogo').addEventListener('click', evt => {
  469. settings.night = !settings.night;
  470. saveData(settings);
  471.  
  472. // Transition to make it feel nicer on the eyes
  473. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '') + '';
  474. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '') + ' *{transition:1s}';
  475.  
  476. if (settings.night) {
  477. updateTheme('/css/?s=36237');
  478. } else {
  479. updateTheme(settings.style == styleOptions.length - 1 ? settings.styleURL : styleUrls[settings.style]);
  480. }
  481.  
  482. setTimeout(() => {
  483. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '');
  484. }, 1000);
  485.  
  486. console.log(`Night mode turned ${settings.night ? 'on' : 'off'}.`);
  487. });
  488.  
  489. if (location.pathname === "/" || location.pathname === "/preview/") {
  490. if (location.search) {
  491. // Remove the current theme if the adventure has CSS (to prevent conflicts);
  492. if (settings.style > 0) {
  493. pageLoad(() => {
  494. if (window.MSPFA) {
  495. if (window.MSPFA.story && window.MSPFA.story.y && (window.MSPFA.story.y.toLowerCase().includes('import') || window.MSPFA.story.y.includes('{'))) {
  496. if (!settings.night) updateTheme('');
  497. return true;
  498. }
  499. }
  500. if (pageLoaded) return true;
  501. });
  502. }
  503.  
  504. // Preload the next page
  505. if (settings.preload) {
  506. const preloadImages = document.createElement('div');
  507. preloadImages.id = 'preload';
  508. preloadImages.style.display = 'none';
  509. document.querySelector('#container').appendChild(preloadImages);
  510.  
  511. window.MSPFA.slide.push(p => {
  512. preloadImages.innerHTML = '';
  513. if (window.MSPFA.story.p[p-2]) {
  514. window.MSPFA.parseBBCode(window.MSPFA.story.p[p-2].b).querySelectorAll('img').forEach(image => {
  515. preloadImages.appendChild(image);
  516. });
  517. }
  518. if (window.MSPFA.story.p[p]) {
  519. window.MSPFA.parseBBCode(window.MSPFA.story.p[p].b).querySelectorAll('img').forEach(image => {
  520. preloadImages.appendChild(image);
  521. });
  522. }
  523. });
  524. }
  525.  
  526. // Scroll up to the nav bar when changing page so you don't have to scroll down as much =)
  527. if (settings.commandScroll) {
  528. const heightTop = document.querySelector('nav').getBoundingClientRect().top - document.body.getBoundingClientRect().top;
  529. let temp = -2; // To prevent moving the page down when loading it for the first time
  530. if (settings.textFix) temp--; // Text Fix adds a page load, so it needs an extra value to not activate
  531. window.MSPFA.slide.push((p) => {
  532. if (temp < 0) {
  533. temp++;
  534. } else {
  535. window.scroll(0, heightTop);
  536. }
  537. });
  538. }
  539.  
  540. // Automatic spoiler opening
  541. if (settings.autospoiler) {
  542. window.MSPFA.slide.push((p) => {
  543. document.querySelectorAll('#slide .spoiler:not(.open) > div:first-child > input').forEach(sb => sb.click());
  544. });
  545. }
  546.  
  547. // Show creation date
  548. pageLoad(() => {
  549. if (document.querySelector('#infobox tr td:nth-child(2)')) {
  550. document.querySelector('#infobox tr td:nth-child(2)').appendChild(document.createTextNode('Creation date: ' + new Date(window.MSPFA.story.d).toString().split(' ').splice(1, 3).join(' ')));
  551. return true;
  552. }
  553. });
  554.  
  555. // Hash scrolling and opening infobox or commmentbox
  556. if (['#infobox', '#commentbox', '#newcomment', '#latestpages'].indexOf(hashSearch) !== -1) {
  557. pageLoad(() => {
  558. if (document.querySelector(hashSearch)) {
  559. if (hashSearch === '#infobox') {
  560. document.querySelector('input[data-open="Show Adventure Info"]').click();
  561. } else if (hashSearch === '#commentbox' || hashSearch === '#newcomment') {
  562. document.querySelector('input[data-open="Show Comments"]').click();
  563. } else if (hashSearch === '#latestpages') {
  564. document.querySelector('input[data-open="Show Adventure Info"]').click();
  565. document.querySelector('input[data-open="Show Latest Pages"]').click();
  566. }
  567. return true;
  568. }
  569. });
  570. }
  571.  
  572. // Attempt to fix text errors
  573. if (settings.textFix) {
  574. pageLoad(() => {
  575. if (window.MSPFA.story && window.MSPFA.story.p) {
  576. // russian/bulgarian is not possible =(
  577. const currentPage = parseInt(/^\?s(?:.*?)&p=([\d]*)$/.exec(location.search)[1]);
  578. const library = [
  579. ["&acirc;��", "'"],
  580. ["&Atilde;�", "Ñ"],
  581. ["&Atilde;&plusmn;", "ñ"],
  582. ["&Atilde;&sup3;", "ó"],
  583. ["&Atilde;&iexcl;", "á"],
  584. ["&Auml;�", "ą"],
  585. ["&Atilde;&shy;", "í"],
  586. ["&Atilde;&ordm;", "ú"],
  587. ["&Atilde;&copy;", "é"],
  588. ["&Aring;�", "ł"],
  589. ["&Aring;&frac14;", "ż"],
  590. ["&Acirc;&iexcl;", "¡"],
  591. ["&Acirc;&iquest;", "¿"],
  592. ["N&Acirc;&ordm;", "#"]
  593. ];
  594. // https://mspfa.com/?s=5280&p=51 -- unknown error
  595.  
  596. const replaceTerms = (p) => {
  597. library.forEach(term => {
  598. if (window.MSPFA.story.p[p]) {
  599. window.MSPFA.story.p[p].c = window.MSPFA.story.p[p].c.replace(new RegExp(term[0], 'g'), term[1]);
  600. window.MSPFA.story.p[p].b = window.MSPFA.story.p[p].b.replace(new RegExp(term[0], 'g'), term[1]);
  601. }
  602. });
  603. };
  604.  
  605. replaceTerms(currentPage-1);
  606.  
  607. window.MSPFA.slide.push(p => {
  608. replaceTerms(p);
  609. replaceTerms(p-2);
  610. });
  611. window.MSPFA.page(currentPage);
  612. return true;
  613. }
  614. });
  615. }
  616.  
  617. // Turn buttons into links
  618. const pageButton = document.createElement('button');
  619. const pageLink = document.createElement('a');
  620. pageLink.href = `/my/stories/pages/?s=${params.s}#p${params.p}`;
  621. pageButton.className = 'pages edit major';
  622. pageButton.type = 'button';
  623. pageButton.title = 'Edit Pages';
  624. pageLink.style.marginRight = '9.5px';
  625. pageButton.style.backgroundImage = 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAL1JREFUOI1jYKAQMCJzugoL/+NSePPhQ4a569YxoouzoAuU9vUx3AoLY1BbtYqBgYGB4d21a8jS/9ENwTBg/ezZDNpQGh2oy8szJAcFoRiCYUBgairDrd27Gdz9/Bh+vH2LInfn+nUMQ3G6YOemTRiKsQEMA9z9/Bie7N7NYG9tjaGYKBfs3LSJQZuBgeHg0aPkucDe2prhDSUuOHj06Ih3wdw5cxgYZGQYGObMId0FNx8+JKhBQlGRKIOJBgBpTFVYzRG//QAAAABJRU5ErkJggg==")';
  626. pageLink.appendChild(pageButton);
  627.  
  628. // Edit pages button & button link
  629. pageLoad(() => {
  630. const infoButton = document.querySelector('.edit.major');
  631. if (infoButton) {
  632. pageLoad(() => {
  633. if (window.MSPFA.me.i) {
  634. infoButton.title = "Edit Info";
  635. infoButton.parentNode.insertBefore(pageLink, infoButton);
  636. addLink(infoButton, `/my/stories/info/?s=${params.s}`);
  637. pageButton.style.display = document.querySelector('.edit.major:not(.pages)').style.display;
  638.  
  639. // Change change page link when switching pages
  640. window.MSPFA.slide.push(p => {
  641. const newSearch = location.search.split('&p=');
  642. pageLink.href = `/my/stories/pages/?s=${params.s}#p${newSearch[1].split('#')[0]}`;
  643. });
  644. return true;
  645. }
  646. if (pageLoaded) return true;
  647. });
  648. addLink(document.querySelector('.rss.major'), `/rss/?s=${params.s}`);
  649. return true;
  650. }
  651. });
  652.  
  653. // Add "Reply" button to comment gear
  654. document.body.addEventListener('click', evt => {
  655. if (evt.toElement.classList.contains('gear')) {
  656. const userID = evt.path[2].classList[2].replace('u', '');
  657. const reportButton = document.querySelector('#dialog button[data-value="Report"]');
  658. const replyButton = document.createElement('button');
  659. replyButton.classList.add('major');
  660. replyButton.type = 'submit';
  661. replyButton.setAttribute('data-value', 'Reply');
  662. replyButton.textContent = 'Reply';
  663. replyButton.style = 'margin-right: 9.5px';
  664. reportButton.parentNode.insertBefore(replyButton, reportButton);
  665.  
  666. replyButton.addEventListener('click', evt => {
  667. document.querySelector('#dialog button[data-value="Cancel"]').click();
  668. const commentBox = document.querySelector('#commentbox textarea');
  669. commentBox.value = `[user]${userID}[/user], ${commentBox.value}`;
  670. commentBox.focus();
  671. });
  672. } else return;
  673. });/**/
  674. }
  675. }
  676. else if (location.pathname === "/my/") {
  677. const parent = document.querySelector('#editstories').parentNode;
  678. const viewSaves = document.createElement('a');
  679. Object.assign(viewSaves, { id: 'viewsaves', className: 'major', textContent: 'View Adventure Saves' });
  680.  
  681. parent.appendChild(viewSaves);
  682. parent.appendChild(newBr());
  683. parent.appendChild(newBr());
  684.  
  685. pageLoad(() => {
  686. if (window.MSPFA && window.MSPFA.me && window.MSPFA.me.i) {
  687. viewSaves.href = `/?s=36596&p=6`;
  688. return true;
  689. }
  690. });
  691. }
  692. else if (location.pathname === "/my/settings/") { // Custom settings
  693. const saveBtn = document.querySelector('#savesettings');
  694.  
  695. const table = document.querySelector("#editsettings tbody");
  696. let saveTr = table.querySelectorAll("tr");
  697. saveTr = saveTr[saveTr.length - 1];
  698.  
  699. const headerTr = document.createElement('tr');
  700. const header = document.createElement('th');
  701. Object.assign(header, { id: 'extraSettings', textContent: 'Extra Settings' });
  702. headerTr.appendChild(header);
  703.  
  704. const moreTr = document.createElement('tr');
  705. const more = document.createElement('td');
  706. more.textContent = "* This only applies to a select few older adventures that have had their text corrupted. Some punctuation is fixed, as well as regular characters with accents. Currently only some spanish/french is fixable. Russian/Bulgarian is not possible.";
  707. moreTr.appendChild(more);
  708.  
  709. const settingsTr = document.createElement('tr');
  710. const localMsg = document.createElement('span');
  711. const settingsTd = document.createElement('td');
  712. localMsg.innerHTML = "Because this is an extension, any data saved is only <b>locally</b> on this device.<br>Don't forget to <b>save</b> when you've finished making changes!";
  713. const plusTable = document.createElement('table');
  714. const plusTbody = document.createElement('tbody');
  715. plusTable.appendChild(plusTbody);
  716. settingsTd.appendChild(localMsg);
  717. settingsTd.appendChild(newBr());
  718. settingsTd.appendChild(newBr());
  719. settingsTd.appendChild(plusTable);
  720. settingsTr.appendChild(settingsTd);
  721.  
  722. plusTable.style = "text-align: center;";
  723.  
  724. // Create checkbox (soooo much better)
  725. const createCheckbox = (text, checked) => {
  726. const optionTr = plusTbody.insertRow(plusTbody.childNodes.length);
  727. const optionTextTd = optionTr.insertCell(0);
  728. const optionInputTd = optionTr.insertCell(1);
  729. const optionInput = document.createElement('input');
  730. optionInputTd.appendChild(optionInput);
  731.  
  732. optionTextTd.textContent = text;
  733. optionInput.type = "checkbox";
  734. optionInput.checked = checked;
  735.  
  736. return optionInput;
  737. }
  738.  
  739. const spoilerInput = createCheckbox("Automatically open spoilers:", settings.autospoiler);
  740. const preloadInput = createCheckbox("Preload images for the pages immediately before and after:", settings.preload);
  741. const errorInput = createCheckbox("Automatically reload Cloudflare 502 error pages:", settings.auto502);
  742. const commandScrollInput = createCheckbox("Scroll back up to the nav bar when switching page:", settings.commandScroll);
  743. const updateInput = createCheckbox("Automatically check for updates:", settings.autoUpdate);
  744. const pixelFixInput = createCheckbox("Change pixel scaling to nearest neighbour:", settings.pixelFix);
  745. const textFixInput = createCheckbox("Attempt to fix text errors (experimental)*:", settings.textFix);
  746.  
  747. const cssTr = plusTbody.insertRow(plusTbody.childNodes.length);
  748. const cssTextTd = cssTr.insertCell(0);
  749. const cssSelectTd = cssTr.insertCell(1);
  750. const cssSelect = document.createElement('select');
  751. cssSelectTd.appendChild(cssSelect);
  752.  
  753. cssTextTd.textContent = "Change style:";
  754.  
  755. const customTr = plusTbody.insertRow(plusTbody.childNodes.length);
  756. const customTextTd = customTr.insertCell(0);
  757. const customCssTd = customTr.insertCell(1);
  758. const customCssInput = document.createElement('input');
  759. customCssTd.appendChild(customCssInput);
  760.  
  761. customTextTd.textContent = "Custom CSS URL:";
  762. customCssInput.style.width = "99px";
  763. customCssInput.value = settings.styleURL;
  764.  
  765. styleOptions.forEach(o => cssSelect.appendChild(new Option(o, o)));
  766.  
  767. saveTr.parentNode.insertBefore(headerTr, saveTr);
  768. saveTr.parentNode.insertBefore(settingsTr, saveTr);
  769. saveTr.parentNode.insertBefore(moreTr, saveTr);
  770. cssSelect.selectedIndex = settings.style;
  771.  
  772. const buttonSpan = document.createElement('span');
  773. const draftButton = document.createElement('input');
  774. const spoilerButton = document.createElement('input');
  775. draftButton.style = 'margin: 0 9.5px;';
  776. draftButton.value = 'Manage Drafts';
  777. draftButton.className = 'major';
  778. draftButton.type = 'button';
  779. spoilerButton.value = 'Manage Spoiler Values';
  780. spoilerButton.className = 'major';
  781. spoilerButton.type = 'button';
  782. buttonSpan.appendChild(draftButton);
  783. buttonSpan.appendChild(spoilerButton);
  784. settingsTd.appendChild(buttonSpan);
  785.  
  786. const draftMsg = window.MSPFA.parseBBCode('Here you can manage the drafts that you have saved for your adventure(s).\n');
  787. const listTable = document.createElement('table');
  788. listTable.style = 'max-height: 250px; overflow-y: scroll; border: 1px solid grey; padding: 2px; width: 100%; text-align: center; vertical-align: middle;';
  789. const listTbody = document.createElement('tbody');
  790. listTable.appendChild(listTbody);
  791.  
  792. if (Object.keys(drafts).length === 0) {
  793. draftButton.disabled = true;
  794. }
  795.  
  796. draftButton.addEventListener('click', () => {
  797. draftMsg.appendChild(listTable);
  798. listTbody.innerHTML = '';
  799. Object.keys(drafts).forEach(adv => {
  800. window.MSPFA.request(0, {
  801. do: "story",
  802. s: adv
  803. }, story => {
  804. if (typeof story !== "undefined") {
  805. const storyTr = listTbody.insertRow(listTable.rows);
  806. const titleLink = document.createElement('a');
  807. Object.assign(titleLink, { className: 'major', href: `/my/stories/pages/?s=${adv}&click=d`, textContent: story.n, target: '_blank' });
  808. storyTr.insertCell(0).appendChild(titleLink);
  809. const deleteButton = document.createElement('input');
  810. Object.assign(deleteButton, { className: 'major', type: 'button', value: 'Delete' });
  811. storyTr.insertCell(1).appendChild(deleteButton);
  812.  
  813. deleteButton.addEventListener('click', () => {
  814. setTimeout(() => {
  815. window.MSPFA.dialog('Delete adventure draft?', document.createTextNode('Are you really sure?\nThis action cannot be undone!'), ["Yes", "No"], (output, form) => {
  816. if (output === "Yes") {
  817. delete drafts[adv];
  818. saveDrafts(drafts);
  819.  
  820. setTimeout(() => {
  821. draftButton.click();
  822. }, 1);
  823.  
  824. if (Object.keys(drafts).length === 0) {
  825. draftButton.disabled = true;
  826. }
  827. }
  828. });
  829. }, 1);
  830. });
  831. }
  832. });
  833. });
  834.  
  835. window.MSPFA.dialog('Manage Drafts', draftMsg, ["Delete All", "Close"], (output, form) => {
  836. if (output === "Delete All") {
  837. setTimeout(() => {
  838. window.MSPFA.dialog('Delete all Drafts?', document.createTextNode('Are you really sure?\nThis action cannot be undone!'), ["Yes", "No"], (output, form) => {
  839. if (output === "Yes") {
  840. drafts = {};
  841. saveDrafts(drafts);
  842. draftButton.disabled = true;
  843. }
  844. });
  845. }, 1);
  846. }
  847. });
  848. });
  849.  
  850. if (Object.keys(settings.spoilerValues).length === 0) {
  851. spoilerButton.disabled = true;
  852. }
  853.  
  854. const spoilerMsg = window.MSPFA.parseBBCode('Here you can manage the spoiler values that you have set for your adventure(s).\nClick on an adventure\'s title to see the values.\n');
  855.  
  856. spoilerButton.addEventListener('click', () => {
  857. spoilerMsg.appendChild(listTable);
  858. listTbody.innerHTML = '';
  859. Object.keys(settings.spoilerValues).forEach(adv => {
  860. window.MSPFA.request(0, {
  861. do: "story",
  862. s: adv
  863. }, story => {
  864. if (typeof story !== "undefined") {
  865. const storyTr = listTbody.insertRow(listTable.rows);
  866. const titleLink = document.createElement('a');
  867. Object.assign(titleLink, { className: 'major', href: `/my/stories/pages/?s=${adv}&click=s`, textContent: story.n, target: '_blank' });
  868. storyTr.insertCell(0).appendChild(titleLink);
  869. const deleteButton = document.createElement('input');
  870. Object.assign(deleteButton, { className: 'major', type: 'button', value: 'Delete' });
  871. storyTr.insertCell(1).appendChild(deleteButton);
  872.  
  873. deleteButton.addEventListener('click', () => {
  874. setTimeout(() => {
  875. window.MSPFA.dialog('Delete adventure spoilers?', document.createTextNode('Are you really sure?\nThis action cannot be undone!'), ["Yes", "No"], (output, form) => {
  876. if (output === "Yes") {
  877. delete settings.spoilerValues[adv];
  878. saveData(settings);
  879.  
  880. setTimeout(() => {
  881. spoilerButton.click();
  882. }, 1);
  883.  
  884. if (Object.keys(settings.spoilerValues).length === 0) {
  885. spoilerButton.disabled = true;
  886. }
  887. }
  888. });
  889. }, 1);
  890. });
  891. }
  892. });
  893. });
  894. window.MSPFA.dialog('Manage Spoiler Values', spoilerMsg, ["Delete All", "Close"], (output, form) => {
  895. if (output === "Delete All") {
  896. setTimeout(() => {
  897. window.MSPFA.dialog('Delete all Spoiler Values?', 'Are you sure you want to delete all spoiler values?\nThis action cannot be undone!', ["Yes", "No"], (output, form) => {
  898. if (output === "Yes") {
  899. settings.spoilerValues = {};
  900. saveData(settings);
  901. draftButton.disabled = true;
  902. }
  903. });
  904. }, 1);
  905. }
  906. });
  907. });
  908.  
  909. // Add event listeners
  910. plusTbody.querySelectorAll('input, select').forEach(elm => {
  911. elm.addEventListener("change", () => {
  912. saveBtn.disabled = false;
  913. });
  914. });
  915.  
  916. saveBtn.addEventListener('mouseup', () => {
  917. settings.autospoiler = spoilerInput.checked;
  918. settings.style = cssSelect.selectedIndex;
  919. settings.styleURL = customCssInput.value;
  920. settings.auto502 = errorInput.checked;
  921. settings.textFix = textFixInput.checked;
  922. settings.pixelFix = pixelFixInput.checked;
  923. settings.autoUpdate = updateInput.checked;
  924. settings.commandScroll = commandScrollInput.checked;
  925. settings.preload = preloadInput.checked;
  926. settings.night = false;
  927. console.log(settings);
  928. saveData(settings);
  929.  
  930. updateTheme(settings.style == styleOptions.length - 1 ? settings.styleURL : styleUrls[settings.style]);
  931.  
  932. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '');
  933.  
  934. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '') + ' *{transition:1s}';
  935. setTimeout(() => {
  936. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '');
  937. }, 1000);
  938. });
  939. }
  940. else if (location.pathname === "/my/messages/") { // New buttons
  941. const btnStyle = "margin: 10px 5px;";
  942.  
  943. // Select all read messages button.
  944. const selRead = document.createElement('input');
  945. selRead.style = btnStyle;
  946. selRead.value = "Select Read";
  947. selRead.id = "selectread";
  948. selRead.classList.add("major");
  949. selRead.type = "button";
  950.  
  951. // On click, select all messages with the style attribute indicating it as read.
  952. selRead.addEventListener('mouseup', () => {
  953. document.querySelectorAll('td[style="border-left: 8px solid rgb(221, 221, 221);"] > input').forEach((m) => m.click());
  954. });
  955.  
  956. // Select duplicate message (multiple update notifications).
  957. const selDupe = document.createElement('input');
  958. selDupe.style = btnStyle;
  959. selDupe.value = "Select Same";
  960. selDupe.id = "selectdupe";
  961. selDupe.classList.add("major");
  962. selDupe.type = "button";
  963.  
  964. selDupe.addEventListener('mouseup', evt => {
  965. const temp = document.querySelectorAll('#messages > tr');
  966. const msgs = [];
  967. for (let i = temp.length - 1; i >= 0; i--) {
  968. msgs.push(temp[i]);
  969. }
  970. const titles = [];
  971. msgs.forEach((msg) => {
  972. let title = msg.querySelector('a.major').textContent;
  973. if (/^New update: /.test(title)) { // Select only adventure updates
  974. if (titles.indexOf(title) === -1) {
  975. if (msg.querySelector('td').style.cssText !== "border-left: 8px solid rgb(221, 221, 221);") {
  976. titles.push(title);
  977. }
  978. } else {
  979. msg.querySelector('input').click();
  980. }
  981. }
  982. });
  983. });
  984.  
  985. // Maybe add a "Merge Updates" button?
  986. // [Merge Updates] would create a list of updates, similar to [Select Same]
  987.  
  988. // Add buttons to the page.
  989. const del = document.querySelector('#deletemsgs');
  990. del.parentNode.appendChild(newBr());
  991. del.parentNode.appendChild(selRead);
  992. del.parentNode.appendChild(selDupe);
  993. }
  994. else if (location.pathname === "/my/messages/new/" && location.search) { // Auto-fill user when linked from a user page
  995. const recipientInput = document.querySelector('#addrecipient');
  996. recipientInput.value = params.u;
  997. pageLoad(() => {
  998. const recipientButton = document.querySelector('#addrecipientbtn');
  999. if (recipientButton) {
  1000. recipientButton.click();
  1001. if (recipientInput.value === "") { // If the button press doesn't work
  1002. return true;
  1003. }
  1004. }
  1005. });
  1006. }
  1007. else if (location.pathname === "/my/stories/") {
  1008. // Add links to buttons
  1009. pageLoad(() => {
  1010. const adventures = document.querySelectorAll('#stories tr');
  1011. if (adventures.length > 0) {
  1012. adventures.forEach(story => {
  1013. const buttons = story.querySelectorAll('input.major');
  1014. const id = story.querySelector('a').href.replace('https://mspfa.com/', '').replace('&p=1', '');
  1015. if (id) {
  1016. addLink(buttons[0], `/my/stories/info/${id}`);
  1017. addLink(buttons[1], `/my/stories/pages/${id}`);
  1018. }
  1019. });
  1020. return true;
  1021. }
  1022. if (pageLoaded) return true;
  1023. });
  1024.  
  1025. // Add user guides
  1026. const guides = ["A Guide To Uploading Your Comic To MSPFA", "MSPFA Etiquette", "Fanventure Guide for Dummies", "CSS Guide", "HTML and CSS Things", ];
  1027. const links = ["https://docs.google.com/document/d/17QI6Cv_BMbr8l06RrRzysoRjASJ-ruWioEtVZfzvBzU/edit?usp=sharing", "/?s=27631", "/?s=29299", "/?s=21099", "/?s=23711"];
  1028. const authors = ["Farfrom Tile", "Radical Dude 42", "nzar", "MadCreativity", "seymour schlong"];
  1029.  
  1030. const parentTd = document.querySelector('.container > tbody > tr:last-child > td');
  1031. const unofficial = parentTd.querySelector('span');
  1032. unofficial.textContent = "Unofficial Guides";
  1033. const guideTable = document.createElement('table');
  1034. const guideTbody = document.createElement('tbody');
  1035. guideTable.style.width = "100%";
  1036. guideTable.style.textAlign = "center";
  1037.  
  1038. guideTable.appendChild(guideTbody);
  1039. parentTd.appendChild(guideTable);
  1040.  
  1041. for (let i = 0; i < guides.length; i++) {
  1042. const guideTr = guideTbody.insertRow(i);
  1043. const guideTd = guideTr.insertCell(0);
  1044. const guideLink = document.createElement('a');
  1045. guideLink.href = links[i];
  1046. guideLink.textContent = guides[i];
  1047. guideLink.className = "major";
  1048. guideTd.appendChild(guideLink);
  1049. guideTd.appendChild(newBr());
  1050. guideTd.appendChild(document.createTextNode('by '+authors[i]));
  1051. guideTd.appendChild(newBr());
  1052. guideTd.appendChild(newBr());
  1053. }
  1054. }
  1055. else if (location.pathname === "/my/stories/info/" && location.search) {
  1056. // Button links
  1057. addLink(document.querySelector('#userfavs'), `/readers/?s=${params.s}`);
  1058. addLink(document.querySelector('#editpages'), `/my/stories/pages/?s=${params.s}`);
  1059. }
  1060. else if (location.pathname === "/my/stories/pages/" && location.search) {
  1061. const adventureID = params.s;
  1062.  
  1063. if (!drafts[adventureID]) {
  1064. drafts[adventureID] = {}
  1065. }
  1066.  
  1067. // Button links
  1068. addLink(document.querySelector('#editinfo'), `/my/stories/info/?s=${adventureID}`);
  1069.  
  1070. // Default spoiler values
  1071. const replaceButton = document.querySelector('#replaceall');
  1072. const spoilerButton = document.createElement('input');
  1073. spoilerButton.classList.add('major');
  1074. spoilerButton.value = 'Default Spoiler Values';
  1075. spoilerButton.type = 'button';
  1076. spoilerButton.id = 'spoilers';
  1077. replaceButton.parentNode.insertBefore(spoilerButton, replaceButton);
  1078. replaceButton.parentNode.insertBefore(newBr(), replaceButton);
  1079. replaceButton.parentNode.insertBefore(newBr(), replaceButton);
  1080.  
  1081. const spoilerSpan = document.createElement('span');
  1082. const spoilerOpen = document.createElement('input');
  1083. const spoilerClose = document.createElement('input');
  1084. spoilerSpan.appendChild(document.createTextNode('Open button text:'));
  1085. spoilerSpan.appendChild(newBr());
  1086. spoilerSpan.appendChild(spoilerOpen);
  1087. spoilerSpan.appendChild(newBr());
  1088. spoilerSpan.appendChild(newBr());
  1089. spoilerSpan.appendChild(document.createTextNode('Close button text:'));
  1090. spoilerSpan.appendChild(newBr());
  1091. spoilerSpan.appendChild(spoilerClose);
  1092.  
  1093. if (!settings.spoilerValues[adventureID]) {
  1094. settings.spoilerValues[adventureID] = {
  1095. open: 'Show',
  1096. close: 'Hide'
  1097. }
  1098. }
  1099.  
  1100. spoilerOpen.value = settings.spoilerValues[adventureID].open;
  1101. spoilerClose.value = settings.spoilerValues[adventureID].close;
  1102.  
  1103. spoilerButton.addEventListener('click', evt => {
  1104. window.MSPFA.dialog('Default Spoiler Values', spoilerSpan, ['Save', 'Cancel'], (output, form) => {
  1105. if (output === 'Save') {
  1106. settings.spoilerValues[adventureID].open = spoilerOpen.value === '' ? 'Show' : spoilerOpen.value;
  1107. settings.spoilerValues[adventureID].close = spoilerClose.value === '' ? 'Hide' : spoilerClose.value;
  1108. if (settings.spoilerValues[adventureID].open === 'Show' && settings.spoilerValues[adventureID].close === 'Hide') {
  1109. delete settings.spoilerValues[adventureID];
  1110. }
  1111. saveData(settings);
  1112. }
  1113. });
  1114. });
  1115.  
  1116. document.querySelector('input[title="Spoiler"]').addEventListener('click', evt => {
  1117. document.querySelector('#dialog input[name="open"]').value = settings.spoilerValues[adventureID].open;
  1118. document.querySelector('#dialog input[name="close"]').value = settings.spoilerValues[adventureID].close;
  1119. document.querySelector('#dialog input[name="open"]').placeholder = settings.spoilerValues[adventureID].open;
  1120. document.querySelector('#dialog input[name="close"]').placeholder = settings.spoilerValues[adventureID].close;
  1121. });
  1122.  
  1123. // Buttonless spoilers
  1124. const flashButton = document.querySelector('input[title="Flash');
  1125. const newSpoilerButton = document.createElement('input');
  1126. newSpoilerButton.setAttribute('data-tag', 'Buttonless Spoiler');
  1127. newSpoilerButton.title = 'Buttonless Spoiler';
  1128. newSpoilerButton.type = 'button';
  1129. newSpoilerButton.style = 'background-position: -66px -88px; background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAAAXNSR0IArs4c6QAAARNJREFUOI3V1LFqhEAQBuDfPaxcLkFwsbhgJ2STWlhQG4twvT7IWecZ8iB3L5In2BfIbaVNglaipMkQPAiBMAnkr2Zg52OZYoD/lg0AKKVMkiQmiqK7russ9cMwvPm+v03T9MHzvKtlWUD1OI5nrXVzOUO9AICyLA9cPzXGfFp1XR+54JWllDJcMFkCAIQQr1wwWQIAiqJouWCyBADM8/zCBa+sMAy3XDBZAgCqqnrkgsmiVVxzwSsrjuNbLpgsAQB5nt9zwWSJj77hgskSAOCcO3GpnNbX+Y0jJL57+NNsAEBKuTPGtFrrxlp7yrLsYIxp+74/B0Fws9/vn6SUu2maPKqdc891XR8vZwDAWvsHe+bOOy65ZXyuZ2jeAAAAAElFTkSuQmCC");';
  1130.  
  1131. newSpoilerButton.addEventListener('click', evt => {
  1132. const bbe = document.querySelector('#bbtoolbar').parentNode.querySelector('textarea');
  1133. if (bbe) {
  1134. bbe.focus();
  1135. const start = bbe.selectionStart;
  1136. const end = bbe.selectionEnd;
  1137. bbe.value = bbe.value.slice(0, start) + '<div class="spoiler"><div>' + bbe.value.slice(start, end) + '</div></div>' + bbe.value.slice(end);
  1138. bbe.selectionStart = start + 26;
  1139. bbe.selectionEnd = end + 26;
  1140. }
  1141. });
  1142.  
  1143. flashButton.parentNode.insertBefore(newSpoilerButton, flashButton);
  1144.  
  1145. // Open preview in new tab with middle mouse
  1146. document.body.addEventListener('mouseup', evt => {
  1147. if (evt.toElement.value === "Preview" && evt.button === 1) {
  1148. evt.toElement.click(); // TODO: Find a way to prevent the middle mouse scroll after clicking there.
  1149. evt.preventDefault();
  1150. return false;
  1151. }
  1152. });
  1153.  
  1154. // -- Drafts --
  1155. // Accessing draft text
  1156. const accessDraftsButton = document.createElement('input');
  1157. accessDraftsButton.classList.add('major');
  1158. accessDraftsButton.value = 'Saved Drafts';
  1159. accessDraftsButton.type = 'button';
  1160. accessDraftsButton.id = 'drafts';
  1161. replaceButton.parentNode.insertBefore(accessDraftsButton, replaceButton);
  1162. accessDraftsButton.parentNode.insertBefore(newBr(), replaceButton);
  1163. accessDraftsButton.parentNode.insertBefore(newBr(), replaceButton);
  1164.  
  1165. accessDraftsButton.addEventListener('click', () => {
  1166. const draftDialog = window.MSPFA.parseBBCode('Use the textbox below to copy out the data and save to a file somewhere else.\nYou can also paste in data to replace the current drafts to ones stored there.');
  1167. const draftInputTextarea = document.createElement('textarea');
  1168. draftInputTextarea.placeholder = 'Paste your draft data here';
  1169. draftInputTextarea.style = 'width: 100%; box-sizing: border-box; resize: vertical;';
  1170. draftInputTextarea.rows = 8;
  1171. draftDialog.appendChild(newBr());
  1172. draftDialog.appendChild(newBr());
  1173. draftDialog.appendChild(draftInputTextarea);
  1174. setTimeout(() => {
  1175. draftInputTextarea.focus();
  1176. draftInputTextarea.selectionStart = 0;
  1177. draftInputTextarea.selectionEnd = 0;
  1178. draftInputTextarea.scrollTop = 0;
  1179. }, 1);
  1180.  
  1181. draftInputTextarea.value = JSON.stringify(drafts[adventureID], null, 4);
  1182.  
  1183. window.MSPFA.dialog('Saved Drafts', draftDialog, ['Load Draft', 'Cancel'], (output, form) => {
  1184. if (output === "Load Draft") {
  1185. if (draftInputTextarea.value === '') {
  1186. setTimeout(() => {
  1187. window.MSPFA.dialog('Saved Drafts', window.MSPFA.parseBBCode('Are you sure you want to delete this adventure\'s draft data?\nMake sure you have it saved somewhere!'), ["Delete", "Cancel"], (output, form) => {
  1188. if (output === "Delete") {
  1189. drafts[adventureID] = {};
  1190. saveDrafts(drafts);
  1191. }
  1192. });
  1193. }, 1);
  1194. } else if (draftInputTextarea.value !== JSON.stringify(drafts[adventureID], null, 4)) {
  1195. setTimeout(() => {
  1196. window.MSPFA.dialog('Saved Drafts', window.MSPFA.parseBBCode('Are you sure you want to load this draft data?\nAll previous draft data for this adventure will be lost!'), ["Load", "Cancel"], (output, form) => {
  1197. if (output === "Load") {
  1198. let newData = {};
  1199. try { // Just in case the data given is invalid.
  1200. newData = JSON.parse(draftInputTextarea.value);
  1201. } catch (err) {
  1202. console.error(err);
  1203. setTimeout(() => {
  1204. window.MSPFA.dialog('Error', window.MSPFA.parseBBCode('The entered data is invalid.'), ["Okay"]);
  1205. }, 1);
  1206. return;
  1207. }
  1208.  
  1209. drafts[adventureID] = newData;
  1210. saveDrafts(drafts);
  1211. }
  1212. });
  1213. }, 1);
  1214. }
  1215. }
  1216. });
  1217. });
  1218.  
  1219. // Draft stuff
  1220. const msg = document.createElement('span');
  1221. msg.appendChild(document.createTextNode('Command:'));
  1222. msg.appendChild(document.createElement('br'));
  1223.  
  1224. const commandInput = document.createElement('input');
  1225. commandInput.style = 'width: 100%; box-sizing: border-box;';
  1226. commandInput.readOnly = true;
  1227. commandInput.value = 'yes';
  1228.  
  1229. msg.appendChild(commandInput);
  1230. msg.appendChild(document.createElement('br'));
  1231. msg.appendChild(document.createElement('br'));
  1232.  
  1233. msg.appendChild(document.createTextNode('Body:'));
  1234.  
  1235. const bodyInput = document.createElement('textarea');
  1236. bodyInput.style = 'width: 100%; box-sizing: border-box; resize: vertical;';
  1237. bodyInput.readOnly = true;
  1238. bodyInput.rows = 8;
  1239. bodyInput.textContent = '';
  1240.  
  1241. msg.appendChild(bodyInput);
  1242.  
  1243. const showDraftDialog = (pageNum) => {
  1244. const pageElement = document.querySelector(`#p${pageNum}`);
  1245.  
  1246. let shownMessage = msg;
  1247. let optionButtons = [];
  1248.  
  1249. const commandElement = pageElement.querySelector('input[name="cmd"]');
  1250. const pageContentElement = pageElement.querySelector('textarea[name="body"]');
  1251.  
  1252. if (typeof drafts[adventureID][pageNum] === "undefined") {
  1253. shownMessage = document.createTextNode('There is no draft saved for this page.');
  1254. optionButtons = ["Save New", "Close"];
  1255. } else {
  1256. commandInput.value = drafts[adventureID][pageNum].command;
  1257. bodyInput.textContent = drafts[adventureID][pageNum].pageContent;
  1258. optionButtons = ["Save New", "Load", "Delete", "Close"];
  1259. }
  1260.  
  1261. window.MSPFA.dialog(`Page ${pageNum} Draft`, shownMessage, optionButtons, (output, form) => {
  1262. if (output === "Save New") {
  1263. if (typeof drafts[adventureID][pageNum] === "undefined") {
  1264. drafts[adventureID][pageNum] = {
  1265. command: commandElement.value,
  1266. pageContent: pageContentElement.value
  1267. }
  1268. saveDrafts(drafts);
  1269. } else {
  1270. setTimeout(() => {
  1271. window.MSPFA.dialog('Overwrite current draft?', document.createTextNode('Doing this will overwrite your current draft with what is currently written in the page box. Are you sure?'), ["Yes", "No"], (output, form) => {
  1272. if (output === "Yes") {
  1273. drafts[adventureID][pageNum] = {
  1274. command: commandElement.value,
  1275. pageContent: pageContentElement.value
  1276. }
  1277. saveDrafts(drafts);
  1278. }
  1279. });
  1280. }, 1);
  1281. }
  1282. } else if (output === "Load") {
  1283. if (pageContentElement.value === '' && (commandElement.value === '' || commandElement.value === document.querySelector('#defaultcmd').value)) {
  1284. commandElement.value = drafts[adventureID][pageNum].command;
  1285. pageContentElement.value = drafts[adventureID][pageNum].pageContent;
  1286. pageElement.querySelector('input[value="Save"]').disabled = false;
  1287. } else {
  1288. setTimeout(() => {
  1289. window.MSPFA.dialog('Overwrite current page?', document.createTextNode('Doing this will overwrite the page\'s content with what is currently written in the draft. Are you sure?'), ["Yes", "No"], (output, form) => {
  1290. if (output === "Yes") {
  1291. commandElement.value = drafts[adventureID][pageNum].command;
  1292. pageContentElement.value = drafts[adventureID][pageNum].pageContent;
  1293. pageElement.querySelector('input[value="Save"]').disabled = false;
  1294. }
  1295. });
  1296. }, 1);
  1297. }
  1298. } else if (output === "Delete") {
  1299. setTimeout(() => {
  1300. window.MSPFA.dialog('Delete this draft?', document.createTextNode('This action is unreversable! Are you sure?'), ["Yes", "No"], (output, form) => {
  1301. if (output === "Yes") {
  1302. delete drafts[adventureID][pageNum];
  1303. saveDrafts(drafts);
  1304. }
  1305. });
  1306. }, 1);
  1307. }
  1308. });
  1309. }
  1310.  
  1311. const createDraftButton = (form) => {
  1312. const draftButton = document.createElement('input');
  1313. draftButton.className = 'major draft';
  1314. draftButton.type = 'button';
  1315. draftButton.value = 'Draft';
  1316. draftButton.style = 'margin-right: 9.5px;';
  1317. draftButton.addEventListener('click', () => {
  1318. showDraftDialog(form.id.replace('p', ''));
  1319. });
  1320. return draftButton;
  1321. }
  1322.  
  1323. pageLoad(() => {
  1324. let allPages = document.querySelectorAll('#storypages form:not(#newpage)');
  1325. if (allPages.length !== 0) {
  1326. allPages.forEach(form => {
  1327. const prevButton = form.querySelector('input[name="preview"]');
  1328. prevButton.parentNode.insertBefore(createDraftButton(form), prevButton);
  1329. });
  1330. document.querySelector('input[value="Add"]').addEventListener('click', () => {
  1331. allPages = document.querySelectorAll('#storypages form:not(#newpage)');
  1332. const form = document.querySelector(`#p${allPages.length}`);
  1333. const prevButton = form.querySelector('input[name="preview"]');
  1334. prevButton.parentNode.insertBefore(createDraftButton(form), prevButton);
  1335. });
  1336. return true;
  1337. }
  1338. });
  1339.  
  1340. if (params.click) {
  1341. if (params.click === 's') {
  1342. spoilerButton.click();
  1343. } else if (params.click === 'd') {
  1344. accessDraftsButton.click();
  1345. }
  1346. }
  1347.  
  1348. /* // Removed because apparently MSPFA already does this fine!
  1349. if (hashSearch) {
  1350. pageLoad(() => {
  1351. const element = document.querySelector(hashSearch);
  1352. if (element) {
  1353. if (element.style.display === "none") {
  1354. element.style = '';
  1355. }
  1356. return true;
  1357. }
  1358. });
  1359. }/**/
  1360. }
  1361. else if (location.pathname === "/my/profile/") {
  1362. // Add profile CSS box
  1363. /*
  1364. pageLoad(() => {
  1365. if (window.MSPFA && window.MSPFA.me && window.MSPFA.me.i) {
  1366. // add 15 to char cap for <style></style>
  1367. // replace
  1368. const bio = document.querySelector('textarea[name="userdesc"]');
  1369. const styleRow = document.querySelector('#editprofile tbody').insertRow(7);
  1370. styleRow.style = 'text-align: left;';
  1371. const styleCell = styleRow.insertCell(0);
  1372. styleCell.colSpan = 2;
  1373. const styleTextarea = document.createElement('textarea');
  1374. styleTextarea.style = 'width: 100%; box-sizing: border-box; resize: vertical;';
  1375. styleTextarea.rows = 8;
  1376. styleCell.appendChild(document.createTextNode('Custom profile style:'));
  1377. styleCell.appendChild(newBr());
  1378. styleCell.appendChild(styleTextarea);
  1379.  
  1380. styleTextarea.value = /<style>(.*?)<\/style>/i.exec(bio.value)[1];
  1381. bio.value = bio.value.replace(/<style>(.*?)<\/style>/i, '');
  1382.  
  1383. return true;
  1384. }
  1385. },0.25);/**/
  1386. }
  1387. else if (location.pathname === "/user/") {
  1388. // Button links
  1389. pageLoad(() => {
  1390. const msgButton = document.querySelector('#sendmsg');
  1391. if (msgButton) {
  1392. addLink(msgButton, `/my/messages/new/?u=${params.u}`);
  1393. addLink(document.querySelector('#favstories'), `/favs/?u=${params.u}`);
  1394. return true;
  1395. }
  1396. });
  1397.  
  1398. // Add extra user stats
  1399. pageLoad(() => {
  1400. if (window.MSPFA) {
  1401. const stats = document.querySelector('#userinfo table');
  1402.  
  1403. const joinTr = stats.insertRow(1);
  1404. const joinTextTd = joinTr.insertCell(0);
  1405. joinTextTd.appendChild(document.createTextNode("Account created:"));
  1406. const joinDate = joinTr.insertCell(1);
  1407. const joinTime = document.createElement('b');
  1408. joinTime.textContent = "Loading...";
  1409. joinDate.appendChild(joinTime);
  1410.  
  1411. const advCountTr = stats.insertRow(2);
  1412. const advTextTd = advCountTr.insertCell(0);
  1413. advTextTd.appendChild(document.createTextNode("Adventures created:"));
  1414. const advCount = advCountTr.insertCell(1);
  1415. const advCountText = document.createElement('b');
  1416. advCountText.textContent = "Loading...";
  1417. advCount.appendChild(advCountText);
  1418.  
  1419. // Show adventure creation date
  1420. window.MSPFA.request(0, {
  1421. do: "user",
  1422. u: params.u
  1423. }, user => {
  1424. if (typeof user !== "undefined") {
  1425. joinTime.textContent = new Date(user.d).toString().split(' ').splice(1, 4).join(' ');
  1426. }
  1427.  
  1428. // Show created adventures
  1429. window.MSPFA.request(0, {
  1430. do: "editor",
  1431. u: params.u
  1432. }, s => {
  1433. if (typeof s !== "undefined") {
  1434. advCountText.textContent = s.length;
  1435. }
  1436.  
  1437. // Show favourites
  1438. if (document.querySelector('#favstories').style.display !== 'none') {
  1439. const favCountTr = stats.insertRow(3);
  1440. const favTextTd = favCountTr.insertCell(0);
  1441. favTextTd.appendChild(document.createTextNode("Adventures favorited:"));
  1442. const favCount = favCountTr.insertCell(1);
  1443. const favCountText = document.createElement('b');
  1444. favCountText.textContent = "Loading...";
  1445. window.MSPFA.request(0, {
  1446. do: "favs",
  1447. u: params.u
  1448. }, s => {
  1449. if (typeof s !== "undefined") {
  1450. favCountText.textContent = s.length;
  1451. }
  1452. });
  1453. favCount.appendChild(favCountText);
  1454. }
  1455. });
  1456. });
  1457.  
  1458. return true;
  1459. }
  1460. });
  1461. }
  1462. else if (location.pathname === "/favs/" && location.search) {
  1463. // Button links
  1464. pageLoad(() => {
  1465. const stories = document.querySelectorAll('#stories tr');
  1466. let favCount = 0;
  1467.  
  1468. if (stories.length > 0) {
  1469. stories.forEach(story => {
  1470. favCount++;
  1471. const id = story.querySelector('a').href.replace('https://mspfa.com/', '');
  1472. pageLoad(() => {
  1473. if (window.MSPFA.me.i) {
  1474. addLink(story.querySelector('.edit.major'), `/my/stories/info/${id}`);
  1475. return true;
  1476. }
  1477. if (pageLoaded) return true;
  1478. });
  1479. addLink(story.querySelector('.rss.major'), `/rss/${id}`);
  1480. });
  1481.  
  1482. // Fav count
  1483. const username = document.querySelector('#username');
  1484. username.parentNode.appendChild(newBr());
  1485. username.parentNode.appendChild(newBr());
  1486. username.parentNode.appendChild(document.createTextNode(`Favorited adventures: ${favCount}`));
  1487.  
  1488. return true;
  1489. }
  1490. if (pageLoaded) return true;
  1491. });
  1492. }
  1493. else if (location.pathname === "/search/" && location.search) {
  1494. // Character and word statistics
  1495. const statTable = document.createElement('table');
  1496. const statTbody = document.createElement('tbody');
  1497. const statTr = statTbody.insertRow(0);
  1498. const charCount = statTr.insertCell(0);
  1499. const wordCount = statTr.insertCell(0);
  1500. const statParentTr = document.querySelector('#pages').parentNode.parentNode.insertRow(2);
  1501. const statParentTd = statParentTr.insertCell(0);
  1502.  
  1503. const statHeaderTr = statTbody.insertRow(0);
  1504. const statHeader = document.createElement('th');
  1505. statHeader.colSpan = '2';
  1506.  
  1507. statHeaderTr.appendChild(statHeader);
  1508. statHeader.textContent = 'Statistics may not be entirely accurate.';
  1509.  
  1510. statTable.style.width = "100%";
  1511.  
  1512. charCount.textContent = "Character count: loading...";
  1513. wordCount.textContent = "Word count: loading...";
  1514.  
  1515. statTable.appendChild(statTbody);
  1516. statParentTd.appendChild(statTable);
  1517.  
  1518. pageLoad(() => {
  1519. if (document.querySelector('#pages br')) {
  1520. const bbc = window.MSPFA.BBC.slice();
  1521. bbc.splice(0, 3);
  1522.  
  1523. window.MSPFA.request(0, {
  1524. do: "story",
  1525. s: params.s
  1526. }, story => {
  1527. if (typeof story !== "undefined") {
  1528. const pageContent = [];
  1529. story.p.forEach(p => {
  1530. pageContent.push(p.c);
  1531. pageContent.push(p.b);
  1532. });
  1533.  
  1534. const storyText = pageContent.join(' ')
  1535. .replace(/\n/g, ' ')
  1536. .replace(bbc[0][0], '$1')
  1537. .replace(bbc[1][0], '$1')
  1538. .replace(bbc[2][0], '$1')
  1539. .replace(bbc[3][0], '$1')
  1540. .replace(bbc[4][0], '$2')
  1541. .replace(bbc[5][0], '$3')
  1542. .replace(bbc[6][0], '$3')
  1543. .replace(bbc[7][0], '$3')
  1544. .replace(bbc[8][0], '$3')
  1545. .replace(bbc[9][0], '$3')
  1546. .replace(bbc[10][0], '$2')
  1547. .replace(bbc[11][0], '$1')
  1548. .replace(bbc[12][0], '$3')
  1549. .replace(bbc[13][0], '$3')
  1550. .replace(bbc[14][0], '')
  1551. .replace(bbc[16][0], '$1')
  1552. .replace(bbc[17][0], '$2 $4 $5')
  1553. .replace(bbc[18][0], '$2 $4 $5')
  1554. .replace(bbc[19][0], '')
  1555. .replace(bbc[20][0], '')
  1556. .replace(/<(.*?)>/g, '');
  1557.  
  1558. wordCount.textContent = `Word count: ${storyText.split(/ +/g).length}`;
  1559. charCount.textContent = `Character count: ${storyText.replace(/ +/g, '').length}`;
  1560. }
  1561. });
  1562. return true;
  1563. }
  1564. });
  1565. }
  1566. else if (location.pathname === "/stories/" && location.search) {
  1567. const adventureList = document.querySelector('#doit');
  1568. const resultAmount = document.createElement('span');
  1569. adventureList.parentNode.appendChild(resultAmount);
  1570.  
  1571. pageLoad(() => {
  1572. if (window.MSPFA) {
  1573. window.MSPFA.request(0, {
  1574. do: "stories",
  1575. n: params.n,
  1576. t: params.t,
  1577. h: params.h,
  1578. o: params.o,
  1579. p: params.p,
  1580. m: 20000
  1581. }, (s) => {
  1582. resultAmount.textContent = `Number of results: ${s.length}`;
  1583. return true;
  1584. });
  1585. return true;
  1586. }
  1587. },1);
  1588.  
  1589. pageLoad(() => {
  1590. const stories = document.querySelector('#stories');
  1591. if (stories.childNodes.length > 0) {
  1592. stories.querySelectorAll('tr').forEach(story => {
  1593. const storyID = story.querySelector('a.major').href.split('&')[0].replace(/\D/g, '');
  1594. addLink(story.querySelector('.rss'), `/rss/?s=${storyID}`);
  1595.  
  1596. pageLoad(() => {
  1597. if (window.MSPFA.me.i) {
  1598. addLink(story.querySelector('.edit.major'), `/my/stories/info/?s=${storyID}`);
  1599. return true;
  1600. }
  1601. if (pageLoaded) return true;
  1602. });
  1603. });
  1604. return true;
  1605. }
  1606. if (pageLoaded) return true;
  1607. });
  1608. }
  1609. })();