MSPFA extras

Adds custom quality of life features to MSPFA.

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

  1. // ==UserScript==
  2. // @name MSPFA extras
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.6.1
  5. // @description Adds custom quality of life features to MSPFA.
  6. // @author seymour schlong
  7. // @icon https://raw.githubusercontent.com/GrantGryczan/MSPFA/master/www/images/ico.svg
  8. // @icon64 https://raw.githubusercontent.com/GrantGryczan/MSPFA/master/www/images/ico.svg
  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. // @grant none
  16. // ==/UserScript==
  17.  
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. const currentVersion = "1.6.1";
  23. console.log(`MSPFA extras script v${currentVersion} by seymour schlong`);
  24.  
  25. /**
  26. * https://github.com/GrantGryczan/MSPFA/projects/1?fullscreen=true
  27. * Github to-do completion list
  28. *
  29. * https://github.com/GrantGryczan/MSPFA/issues/26 - Dropdown menu - February 23rd, 2020
  30. * https://github.com/GrantGryczan/MSPFA/issues/18 - MSPFA themes - February 23rd, 2020
  31. * https://github.com/GrantGryczan/MSPFA/issues/32 - Adventure creation dates - February 23rd, 2020
  32. * https://github.com/GrantGryczan/MSPFA/issues/32 - User creation dates - February 23rd, 2020
  33. * https://github.com/GrantGryczan/MSPFA/issues/40 - Turn certain buttons into links - July 21st, 2020
  34. * https://github.com/GrantGryczan/MSPFA/issues/41 - Word and character count - July 21st, 2020
  35. * https://github.com/GrantGryczan/MSPFA/issues/57 - Default spoiler values - August 7th, 2020
  36. * https://github.com/GrantGryczan/MSPFA/issues/62 - Buttonless spoilers - August 7th, 2020
  37. * https://github.com/GrantGryczan/MSPFA/issues/52 - Hash URLs - August 8th, 2020
  38. * - Page drafts - August 8th, 2020
  39. * - Edit pages button - August 8th, 2020
  40. *
  41. * Extension to-do... maybe...
  42. *
  43. * 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.
  44. */
  45.  
  46. // A general function that allows for waiting until a certain element appears on the page.
  47. const pageLoad = (fn, length) => {
  48. const interval = setInterval(() => {
  49. if (fn()) clearInterval(interval);
  50. }, length ? length*1000 : 500);
  51. };
  52.  
  53. // Saves the options data for the script.
  54. const saveData = (data) => {
  55. localStorage.mspfaextra = JSON.stringify(data);
  56. };
  57.  
  58. // Encases an element within a link
  59. const addLink = (elm, url) => {
  60. const link = document.createElement('a');
  61. link.href = url;
  62. elm.parentNode.insertBefore(link, elm);
  63. link.appendChild(elm);
  64. };
  65.  
  66. // Returns true if version 2 is newer
  67. const compareVer = (ver1, ver2) => {
  68. ver1 = ver1.split(/\./); // current version
  69. ver2 = ver2.split(/\./); // new version
  70. ver1.push(0);
  71. ver1.push(0);
  72. ver2.push(0);
  73. ver2.push(0);
  74. if (parseInt(ver2[0]) > parseInt(ver1[0])) { // 1.x.x
  75. return true;
  76. } else if (parseInt(ver2[1]) > parseInt(ver1[1])) { // x.1.x
  77. return true;
  78. } else if (parseInt(ver2[2]) > parseInt(ver1[2])) { // x.x.1
  79. return true;
  80. } else if (parseInt(ver2[3]) > parseInt(ver1[3])) { // x.x.1
  81. return true;
  82. }
  83. return false;
  84. }
  85.  
  86. // Easy br element
  87. const newBr = () => {
  88. return document.createElement('br');
  89. }
  90.  
  91. let settings = {};
  92. const defaultSettings = {
  93. autospoiler: false,
  94. style: 0,
  95. styleURL: "",
  96. night: false,
  97. auto502: true,
  98. textFix: false,
  99. pixelFix: false,
  100. intro: false,
  101. autoUpdate: false,
  102. version: currentVersion,
  103. spoilerValues: {},
  104. drafts: {}
  105. }
  106.  
  107. // Load any previous settings from localStorage
  108. if (localStorage.mspfaextra) {
  109. Object.assign(settings, JSON.parse(localStorage.mspfaextra));
  110. }
  111.  
  112. // If any settings are undefined, re-set to their default state. (For older users when new things get stored)
  113. const checkSettings = () => {
  114. const defaultSettingsKeys = Object.keys(defaultSettings);
  115. for (let i = 0; i < defaultSettingsKeys.length; i++) {
  116. if (typeof settings[defaultSettingsKeys[i]] === "undefined") {
  117. settings[defaultSettingsKeys[i]] = defaultSettings[defaultSettingsKeys[i]];
  118. }
  119. }
  120. saveData(settings);
  121. }
  122.  
  123. checkSettings();
  124.  
  125. // Update saved version to the version used in the script to prevent unnecessary notifications
  126. if (compareVer(settings.version, currentVersion)) {
  127. settings.version = currentVersion;
  128. saveData(settings);
  129. }
  130.  
  131. // Functions to get/change data from the console
  132. window.MSPFAe = {
  133. getSettings: () => {
  134. return settings;
  135. },
  136. getSettingsString: (formatted) => {
  137. if (formatted) {
  138. console.log(JSON.stringify(settings, null, 4));
  139. } else {
  140. console.log(JSON.stringify(settings));
  141. }
  142. },
  143. changeSettings: (newSettings) => {
  144. console.log('Settings updated');
  145. console.log(settings);
  146. Object.assign(settings, newSettings);
  147. saveData(settings);
  148. },
  149. changeSettingsString: (fullString) => {
  150. try {
  151. JSON.parse(fullString);
  152. } catch (err) {
  153. console.error(err);
  154. return;
  155. }
  156. settings = JSON.parse(fullString);
  157. checkSettings();
  158. console.log(settings);
  159. }
  160. }
  161.  
  162. // Delete any unchanged spoiler values or empty drafts
  163. if (location.pathname !== "/my/stories/pages/") {
  164. // Go through spoiler values and remove any that aren't unique
  165. Object.keys(settings.spoilerValues).forEach(adventure => {
  166. if (settings.spoilerValues[adventure].open === "Show" && settings.spoilerValues[adventure].close === "Hide") {
  167. delete settings.spoilerValues[adventure];
  168. } else if (settings.spoilerValues[adventure].open === '' && settings.spoilerValues[adventure].close === '') {
  169. delete settings.spoilerValues[adventure];
  170. }
  171. });
  172. // Go through and remove adventures with empty drafts
  173. Object.keys(settings.drafts).forEach(adventure => {
  174. if (Object.keys(settings.drafts[adventure]).length === 0) {
  175. delete settings.drafts[adventure];
  176. }
  177. });
  178. }
  179.  
  180. const styleOptions = ["Standard", "Low Contrast", "Light", "Dark", "Felt", "Trickster", "Custom"];
  181. const styleUrls = ['', '/css/theme1.css', '/css/theme2.css', '/css/?s=36237', '/css/theme4.css', '/css/theme5.css'];
  182.  
  183. // Dropdown menu
  184. const myLink = document.querySelector('nav a[href="/my/"]');
  185. if (myLink) {
  186. const dropDiv = document.createElement('div');
  187. dropDiv.className = 'dropdown';
  188. Object.assign(dropDiv.style, {
  189. position: 'relative',
  190. display: 'inline-block',
  191. backgroundColor: 'inherit'
  192. });
  193.  
  194. const dropContent = document.createElement('div');
  195. dropContent.className = 'dropdown-content';
  196. Object.assign(dropContent.style, {
  197. display: 'none',
  198. backgroundColor: 'inherit',
  199. position: 'absolute',
  200. textAlign: 'left',
  201. minWidth: '100px',
  202. marginLeft: '-5px',
  203. padding: '2px',
  204. zIndex: '1',
  205. borderRadius: '0 0 5px 5px'
  206. });
  207.  
  208. dropDiv.addEventListener('mouseenter', evt => {
  209. dropContent.style.display = 'block';
  210. dropContent.style.color = getComputedStyle(myLink).color;
  211. });
  212. dropDiv.addEventListener('mouseleave', evt => {
  213. dropContent.style.display = 'none';
  214. });
  215.  
  216. myLink.parentNode.insertBefore(dropDiv, myLink);
  217. dropDiv.appendChild(myLink);
  218. dropDiv.appendChild(dropContent);
  219.  
  220. const dLinks = [];
  221. dLinks[0] = [ 'Messages', 'My Adventures', 'Settings' ];
  222. dLinks[1] = [ '/my/messages/', '/my/stories/', '/my/settings/' ];
  223.  
  224. for (let i = 0; i < dLinks[0].length; i++) {
  225. const newLink = document.createElement('a');
  226. newLink.textContent = dLinks[0][i];
  227. newLink.href = dLinks[1][i];
  228. dropContent.appendChild(newLink);
  229. }
  230.  
  231. // Append "My Profile" to the dropdown list if you're signed in, or "Login" if you're not
  232. pageLoad(() => {
  233. if (window.MSPFA) {
  234. if (window.MSPFA.me.n) {
  235. const newLink = document.createElement('a');
  236. newLink.textContent = "My Profile";
  237. newLink.href = `/user/?u=${window.MSPFA.me.i}`;
  238. dropContent.appendChild(newLink);
  239. return true;
  240. }
  241. }
  242. });
  243. }
  244.  
  245. const hashSearch = location.href.replace(location.origin + location.pathname, '').replace(location.search, '');
  246. if (hashSearch !== '') {
  247. pageLoad(() => {
  248. const idElement = document.querySelector(hashSearch);
  249. if (idElement) {
  250. document.querySelector(hashSearch).scrollIntoView();
  251. return true;
  252. }
  253. }, 1);
  254. }
  255.  
  256. // Error reloading
  257. window.addEventListener("load", () => {
  258. // Reload the page if 502 CloudFlare error page appears
  259. if (settings.auto502 && document.querySelector('.cf-error-overview')) {
  260. window.location.reload();
  261. }
  262.  
  263. // Wait five seconds, then refresh the page
  264. if (document.body.textContent === "Your client is sending data to MSPFA too quickly. Wait a moment before continuing.") {
  265. setTimeout(() => {
  266. window.location.reload();
  267. }, 5000);
  268. }
  269.  
  270. // Replace "My MSPFA" with "Log in" if the user isn't logged in
  271. if (!window.MSPFA.me.i) {
  272. const dropContent = document.querySelector('.dropdown-content');
  273. console.log(window.MSPFA.me);
  274. dropContent.innerHTML = '';
  275. dropContent.style.padding = '0px';
  276. myLink.textContent = 'Log in';
  277. myLink.href = '/login/';
  278. return true;
  279. }
  280. });
  281.  
  282. // Message that shows when you first get the script
  283. const showIntroDialog = () => {
  284. 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]');
  285. window.MSPFA.dialog("MSPFA extras message", msg, ["Okay"]);
  286. }
  287.  
  288. // Check for updates by comparing currentVersion to text data from an adventure that has update text and info
  289. const checkForUpdates = (evt) => {
  290. window.MSPFA.request(0, {
  291. do: "story",
  292. s: "36596"
  293. }, story => {
  294. if (typeof story !== "undefined") {
  295. const ver = settings.version.split(/\./);
  296. const newVer = story.p[1].c.split(/\./);
  297. // compare versions
  298. if (compareVer(settings.version, story.p[1].c) || (evt && evt.type === 'click')) {
  299. const msg = window.MSPFA.parseBBCode(story.p[1].b);
  300. settings.version = story.p[1].c;
  301. saveData(settings);
  302. window.MSPFA.dialog(`MSPFA extras update! (${story.p[1].c})`, msg, ["Opt-out", "Dismiss", "Update"], (output, form) => {
  303. if (output === "Update") {
  304. window.open('https://greasyfork.org/en/scripts/396798-mspfa-extras', '_blank').focus();
  305. } else if (output === "Opt-out") {
  306. settings.autoUpdate = false;
  307. saveData(settings);
  308. }
  309. });
  310. }
  311. }
  312. });
  313. };
  314.  
  315.  
  316. // Check for updates and show intro dialog if needed
  317. pageLoad(() => {
  318. if (window.MSPFA) {
  319. if (settings.autoUpdate) {
  320. checkForUpdates();
  321. }
  322.  
  323. if (!settings.intro) {
  324. showIntroDialog();
  325. settings.intro = true;
  326. saveData(settings);
  327. }
  328. return true;
  329. }
  330. });
  331.  
  332. const linkColour = document.createElement('a');
  333. linkColour.href = "/";
  334. linkColour.id = "linkColour";
  335. document.body.appendChild(linkColour);
  336. const details = document.querySelector('#details');
  337. // Add 'link' at the bottom to show the intro dialog again
  338. const introLink = document.createElement('a');
  339. introLink.textContent = 'View Script Message';
  340. introLink.style = 'cursor: pointer; text-decoration: underline;';
  341. introLink.style.color = getComputedStyle(linkColour).color;
  342. introLink.className = 'intro-link';
  343. introLink.addEventListener('click', showIntroDialog);
  344. details.appendChild(introLink);
  345.  
  346. // vbar!!!!
  347. const vbar = document.createElement('span');
  348. Object.assign(vbar, {className: 'vbar', style: 'padding: 0 5px', textContent: '|'});
  349. details.appendChild(vbar);
  350.  
  351. // Add 'link' at the bottom to show the update dialog again
  352. const updateLink = document.createElement('a');
  353. updateLink.textContent = 'View Update';
  354. updateLink.style = 'cursor: pointer; text-decoration: underline;';
  355. updateLink.style.color = getComputedStyle(linkColour).color;
  356. updateLink.className = 'intro-link';
  357. updateLink.addEventListener('click', checkForUpdates);
  358. details.appendChild(updateLink);
  359.  
  360. // vbar 2!!!!
  361. const vbar2 = document.createElement('span');
  362. Object.assign(vbar2, {className: 'vbar', style: 'padding: 0 5px', textContent: '|'});
  363. details.appendChild(vbar2);
  364.  
  365. // if you really enjoy the script and has some extra moneys 🥺
  366. const donateLink = document.createElement('a');
  367. donateLink.textContent = 'Donate';
  368. donateLink.href = 'https://ko-fi.com/ironbean';
  369. donateLink.target = "blank";
  370. details.appendChild(donateLink);
  371.  
  372. // Theme stuff
  373. const theme = document.createElement('link');
  374. Object.assign(theme, { id: 'theme', type: 'text/css', rel: 'stylesheet' });
  375. const updateTheme = (src) => {
  376. theme.href = src;
  377. setTimeout(() => {
  378. introLink.style.color = getComputedStyle(linkColour).color;
  379. updateLink.style.color = getComputedStyle(linkColour).color;
  380. }, 1500);
  381. }
  382. if (!document.querySelector('#theme') && !/^\/css\/|^\/js\//.test(location.pathname)) {
  383. document.querySelector('head').appendChild(theme);
  384. if (settings.night) {
  385. updateTheme('/css/?s=36237');
  386. } else {
  387. updateTheme(settings.style == styleOptions.length - 1 ? settings.styleURL : styleUrls[settings.style]);
  388. }
  389. }
  390.  
  391. // Dropdown menu and pixelated scaling
  392. const dropStyle = document.createElement('style');
  393. const pixelFixText = 'img, .mspfalogo, .major, .arrow, #flashytitle, .heart, .fav, .notify, .edit, .rss, input, #loading { image-rendering: pixelated !important; }';
  394. const dropStyleText = `#notification { z-index: 2; } .dropdown-content a { color: inherit; padding: 2px; text-decoration: underline; display: block;}`;
  395. if (!document.querySelector('#dropdown-style')) {
  396. dropStyle.id = 'dropdown-style';
  397. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '');
  398. //dropdownStyle.textContent = '#notification { z-index: 2;}.dropdown:hover .dropdown-content { display: block;}.dropdown { position: relative; display: inline-block; background-color: inherit;}.dropdown-content { display: none; position: absolute; text-align: left; background-color: inherit; min-width: 100px; margin-left: -5px; padding: 2px; z-index: 1; border-radius: 0 0 5px 5px;}.dropdown-content a { color: #fffa36; padding: 2px 2px; text-decoration: underline; display: block;}';
  399.  
  400. document.querySelector('head').appendChild(dropStyle);
  401. }
  402.  
  403. // Remove the current theme if the adventure has CSS (to prevent conflicts);
  404. pageLoad(() => {
  405. if (window.MSPFA) {
  406. if (window.MSPFA.story && window.MSPFA.story.y && window.MSPFA.story.y.replace(/^((?!import).)*/igm, '').length > 0) {
  407. updateTheme('');
  408. }
  409. return true;
  410. }
  411. });
  412.  
  413. // Enabling night mode.
  414. document.querySelector('footer .mspfalogo').addEventListener('click', evt => {
  415. settings.night = !settings.night;
  416. saveData(settings);
  417.  
  418. // Transition to make it feel nicer on the eyes
  419. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '') + '';
  420. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '') + ' *{transition:1s}';
  421.  
  422. if (settings.night) {
  423. updateTheme('/css/?s=36237');
  424. } else {
  425. updateTheme(settings.style == styleOptions.length - 1 ? settings.styleURL : styleUrls[settings.style]);
  426. }
  427.  
  428. setTimeout(() => {
  429. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '');
  430. }, 1000);
  431.  
  432. console.log(`Night mode turned ${settings.night ? 'on' : 'off'}.`);
  433. });
  434.  
  435. if (location.pathname === "/" || location.pathname === "/preview/") {
  436. if (location.search) {
  437. // Automatic spoiler opening
  438. if (settings.autospoiler) {
  439. window.MSPFA.slide.push((p) => {
  440. document.querySelectorAll('#slide .spoiler:not(.open) > div:first-child > input').forEach(sb => sb.click());
  441. });
  442. }
  443.  
  444. // Show creation date
  445. pageLoad(() => {
  446. if (document.querySelector('#infobox tr td:nth-child(2)')) {
  447. 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(' ')));
  448. return true;
  449. }
  450. });
  451.  
  452. // Hash scrolling and opening infobox or commmentbox
  453. if (['#infobox', '#commentbox', '#latestpages'].indexOf(hashSearch) !== -1) {
  454. pageLoad(() => {
  455. if (document.querySelector(hashSearch)) {
  456. if (hashSearch === '#infobox') {
  457. document.querySelector('input[data-open="Show Adventure Info"]').click();
  458. } else if (hashSearch === '#commentbox') {
  459. document.querySelector('input[data-open="Show Comments"]').click();
  460. } else if (hashSearch === '#latestpages') {
  461. document.querySelector('input[data-open="Show Adventure Info"]').click();
  462. document.querySelector('input[data-open="Show Latest Pages"]').click();
  463. }
  464. return true;
  465. }
  466. });
  467. }
  468.  
  469. // Attempt to fix text errors
  470. if (settings.textFix) {
  471. pageLoad(() => {
  472. if (window.MSPFA.story && window.MSPFA.story.p) {
  473. // russian/bulgarian is not possible =(
  474. const currentPage = parseInt(/^\?s(?:.*?)&p=([\d]*)$/.exec(location.search)[1]);
  475. const library = [
  476. ["&acirc;��", "'"],
  477. ["&Atilde;�", "Ñ"],
  478. ["&Atilde;&plusmn;", "ñ"],
  479. ["&Atilde;&sup3;", "ó"],
  480. ["&Atilde;&iexcl;", "á"],
  481. ["&Atilde;&shy;", "í"],
  482. ["&Atilde;&ordm;", "ú"],
  483. ["&Atilde;&copy;", "é"],
  484. ["&Acirc;&iexcl;", "¡"],
  485. ["&Acirc;&iquest;", "¿"],
  486. ["N&Acirc;&ordm;", "#"]
  487. ];
  488. // https://mspfa.com/?s=5280&p=51 -- unknown error
  489.  
  490. const replaceTerms = (p) => {
  491. library.forEach(term => {
  492. if (window.MSPFA.story.p[p]) {
  493. window.MSPFA.story.p[p].c = window.MSPFA.story.p[p].c.replace(new RegExp(term[0], 'g'), term[1]);
  494. window.MSPFA.story.p[p].b = window.MSPFA.story.p[p].b.replace(new RegExp(term[0], 'g'), term[1]);
  495. }
  496. });
  497. };
  498.  
  499. replaceTerms(currentPage-1);
  500.  
  501. window.MSPFA.slide.push(p => {
  502. replaceTerms(p);
  503. replaceTerms(p-2);
  504. });
  505. window.MSPFA.page(currentPage);
  506. return true;
  507. }
  508. });
  509. }
  510.  
  511. // Turn buttons into links
  512. const pageButton = document.createElement('button');
  513. const pageLink = document.createElement('a');
  514. const searchContent = location.search.split('&p=');
  515. pageLink.href = `/my/stories/pages/${searchContent[0]}#p${searchContent[1].split('#')[0]}`;
  516. pageButton.className = 'pages edit major';
  517. pageButton.type = 'button';
  518. pageButton.title = 'Edit Pages';
  519. pageLink.style.marginRight = '9.5px';
  520. pageButton.style.backgroundImage = 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAL1JREFUOI1jYKAQMCJzugoL/+NSePPhQ4a569YxoouzoAuU9vUx3AoLY1BbtYqBgYGB4d21a8jS/9ENwTBg/ezZDNpQGh2oy8szJAcFoRiCYUBgairDrd27Gdz9/Bh+vH2LInfn+nUMQ3G6YOemTRiKsQEMA9z9/Bie7N7NYG9tjaGYKBfs3LSJQZuBgeHg0aPkucDe2prhDSUuOHj06Ih3wdw5cxgYZGQYGObMId0FNx8+JKhBQlGRKIOJBgBpTFVYzRG//QAAAABJRU5ErkJggg==")';
  521. pageLink.appendChild(pageButton);
  522.  
  523. pageLoad(() => {
  524. const infoButton = document.querySelector('.edit.major');
  525. if (infoButton) {
  526. pageLoad(() => {
  527. if (window.MSPFA.me.i) {
  528. infoButton.title = "Edit Info";
  529. infoButton.parentNode.insertBefore(pageLink, infoButton);
  530. addLink(infoButton, `/my/stories/info/${searchContent[0]}`);
  531. pageButton.style.display = document.querySelector('.edit.major:not(.pages)').style.display;
  532. return true;
  533. }
  534. });
  535. addLink(document.querySelector('.rss.major'), `/rss/${searchContent[0]}`);
  536. return true;
  537. }
  538. });
  539. window.addEventListener('load', evt => {
  540. const favButton = document.querySelector('.fav');
  541. window.MSPFA.slide.push(p => {
  542. const newSearch = location.search.split('&p=');
  543. pageLink.href = `/my/stories/pages/${newSearch[0]}#p${newSearch[1].split('#')[0]}`;
  544. });
  545. });
  546.  
  547. // Add "Reply" button to comment gear
  548. document.body.addEventListener('click', evt => {
  549. if (evt.toElement.classList.contains('gear')) {
  550. const userID = evt.path[2].classList[2].replace('u', '');
  551. const reportButton = document.querySelector('#dialog button[data-value="Report"]');
  552. const replyButton = document.createElement('button');
  553. replyButton.classList.add('major');
  554. replyButton.type = 'submit';
  555. replyButton.setAttribute('data-value', 'Reply');
  556. replyButton.textContent = 'Reply';
  557. replyButton.style = 'margin-right: 9.5px';
  558. reportButton.parentNode.insertBefore(replyButton, reportButton);
  559.  
  560. replyButton.addEventListener('click', evt => {
  561. document.querySelector('#dialog button[data-value="Cancel"]').click();
  562. const commentBox = document.querySelector('#commentbox textarea');
  563. commentBox.value = `[user]${userID}[/user], ${commentBox.value}`;
  564. commentBox.focus();
  565. // Weird bug where if you have JS console open it opens debugger?
  566. });
  567. } else return;
  568. });/**/
  569. }
  570. }
  571. else if (location.pathname === "/my/settings/") { // Custom settings
  572. const saveBtn = document.querySelector('#savesettings');
  573.  
  574. const table = document.querySelector("#editsettings tbody");
  575. let saveTr = table.querySelectorAll("tr");
  576. saveTr = saveTr[saveTr.length - 1];
  577.  
  578. const headerTr = document.createElement('tr');
  579. const header = document.createElement('th');
  580. Object.assign(header, { id: 'extraSettings', textContent: 'Extra Settings' });
  581. headerTr.appendChild(header);
  582.  
  583. const moreTr = document.createElement('tr');
  584. const more = document.createElement('td');
  585. 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.";
  586. moreTr.appendChild(more);
  587.  
  588. const settingsTr = document.createElement('tr');
  589. const localMsg = document.createElement('span');
  590. const settingsTd = document.createElement('td');
  591. 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!";
  592. const plusTable = document.createElement('table');
  593. const plusTbody = document.createElement('tbody');
  594. plusTable.appendChild(plusTbody);
  595. settingsTd.appendChild(localMsg);
  596. settingsTd.appendChild(newBr());
  597. settingsTd.appendChild(newBr());
  598. settingsTd.appendChild(plusTable);
  599. settingsTr.appendChild(settingsTd);
  600.  
  601. plusTable.style = "text-align: center;";
  602.  
  603. // Create checkbox (soooo much better)
  604. const createCheckbox = (text, checked) => {
  605. const optionTr = plusTbody.insertRow(plusTbody.childNodes.length);
  606. const optionTextTd = optionTr.insertCell(0);
  607. const optionInputTd = optionTr.insertCell(1);
  608. const optionInput = document.createElement('input');
  609. optionInputTd.appendChild(optionInput);
  610.  
  611. optionTextTd.textContent = text;
  612. optionInput.type = "checkbox";
  613. optionInput.checked = checked;
  614.  
  615. return optionInput;
  616. }
  617.  
  618. const spoilerInput = createCheckbox("Automatically open spoilers:", settings.autospoiler);
  619. const errorInput = createCheckbox("Automatically reload Cloudflare 502 error pages:", settings.auto502);
  620. const updateInput = createCheckbox("Automatically check for updates:", settings.autoUpdate);
  621. const pixelFixInput = createCheckbox("Change pixel scaling to nearest neighbour:", settings.pixelFix);
  622. const textFixInput = createCheckbox("Attempt to fix text errors (experimental)*:", settings.textFix);
  623.  
  624. const cssTr = plusTbody.insertRow(plusTbody.childNodes.length);
  625. const cssTextTd = cssTr.insertCell(0);
  626. const cssSelectTd = cssTr.insertCell(1);
  627. const cssSelect = document.createElement('select');
  628. cssSelectTd.appendChild(cssSelect);
  629.  
  630. cssTextTd.textContent = "Change style:";
  631.  
  632. const customTr = plusTbody.insertRow(plusTbody.childNodes.length);
  633. const customTextTd = customTr.insertCell(0);
  634. const customCssTd = customTr.insertCell(1);
  635. const customCssInput = document.createElement('input');
  636. customCssTd.appendChild(customCssInput);
  637.  
  638. customTextTd.textContent = "Custom CSS URL:";
  639. customCssInput.style.width = "99px";
  640. customCssInput.value = settings.styleURL;
  641.  
  642. styleOptions.forEach(o => cssSelect.appendChild(new Option(o, o)));
  643.  
  644. saveTr.parentNode.insertBefore(headerTr, saveTr);
  645. saveTr.parentNode.insertBefore(settingsTr, saveTr);
  646. saveTr.parentNode.insertBefore(moreTr, saveTr);
  647. cssSelect.selectedIndex = settings.style;
  648.  
  649. const buttonSpan = document.createElement('span');
  650. const draftButton = document.createElement('input');
  651. const spoilerButton = document.createElement('input');
  652. draftButton.style = 'margin: 0 9.5px;';
  653. draftButton.value = 'Clear Drafts';
  654. draftButton.className = 'major';
  655. draftButton.type = 'button';
  656. spoilerButton.value = 'Clear Spoiler Values';
  657. spoilerButton.className = 'major';
  658. spoilerButton.type = 'button';
  659. buttonSpan.appendChild(draftButton);
  660. buttonSpan.appendChild(spoilerButton);
  661. settingsTd.appendChild(buttonSpan);
  662.  
  663. draftButton.addEventListener('click', () => {
  664. window.MSPFA.dialog('Delete all Drafts?', window.MSPFA.parseBBCode(`Doing this will delete all drafts saved for [b]${Object.keys(settings.drafts).length}[/b] adventure(s).\nAre you sure? This action is irreversible.`), ["Yes", "No"], (output, form) => {
  665. if (output === "Yes") {
  666. setTimeout(() => {
  667. window.MSPFA.dialog('Delete all Drafts?', document.createTextNode('Are you really sure?'), ["No", "Yes"], (output, form) => {
  668. if (output === "Yes") {
  669. settings.drafts = {};
  670. saveData(settings);
  671. }
  672. });
  673. }, 1);
  674. }
  675. });
  676. });
  677.  
  678. spoilerButton.addEventListener('click', () => {
  679. window.MSPFA.dialog('Delete all Spoiler Values?', window.MSPFA.parseBBCode(`Doing this will delete all spoiler values saved for [b]${Object.keys(settings.spoilerValues).length}[/b] adventure(s).\nAre you sure? This action is irreversible.`), ["Yes", "No"], (output, form) => {
  680. if (output === "Yes") {
  681. settings.spoilerValues = {};
  682. saveData(settings);
  683. }
  684. });
  685. });
  686.  
  687. // Add event listeners
  688. plusTbody.querySelectorAll('input, select').forEach(elm => {
  689. elm.addEventListener("change", () => {
  690. saveBtn.disabled = false;
  691. });
  692. });
  693.  
  694. saveBtn.addEventListener('mouseup', () => {
  695. settings.autospoiler = spoilerInput.checked;
  696. settings.style = cssSelect.selectedIndex;
  697. settings.styleURL = customCssInput.value;
  698. settings.auto502 = errorInput.checked;
  699. settings.textFix = textFixInput.checked;
  700. settings.pixelFix = pixelFixInput.checked;
  701. settings.autoUpdate = updateInput.checked;
  702. settings.night = false;
  703. console.log(settings);
  704. saveData(settings);
  705.  
  706. updateTheme(settings.style == styleOptions.length - 1 ? settings.styleURL : styleUrls[settings.style]);
  707.  
  708. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '');
  709.  
  710. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '') + ' *{transition:1s}';
  711. setTimeout(() => {
  712. dropStyle.textContent = dropStyleText + (settings.pixelFix ? ' '+pixelFixText : '');
  713. }, 1000);
  714. });
  715. }
  716. else if (location.pathname === "/my/messages/") { // New buttons
  717. const btnStyle = "margin: 10px 5px;";
  718.  
  719. // Select all read messages button.
  720. const selRead = document.createElement('input');
  721. selRead.style = btnStyle;
  722. selRead.value = "Select Read";
  723. selRead.id = "selectread";
  724. selRead.classList.add("major");
  725. selRead.type = "button";
  726.  
  727. // On click, select all messages with the style attribute indicating it as read.
  728. selRead.addEventListener('mouseup', () => {
  729. document.querySelectorAll('td[style="border-left: 8px solid rgb(221, 221, 221);"] > input').forEach((m) => m.click());
  730. });
  731.  
  732. // Select duplicate message (multiple update notifications).
  733. const selDupe = document.createElement('input');
  734. selDupe.style = btnStyle;
  735. selDupe.value = "Select Same";
  736. selDupe.id = "selectdupe";
  737. selDupe.classList.add("major");
  738. selDupe.type = "button";
  739.  
  740. selDupe.addEventListener('mouseup', evt => {
  741. const temp = document.querySelectorAll('#messages > tr');
  742. const msgs = [];
  743. for (let i = temp.length - 1; i >= 0; i--) {
  744. msgs.push(temp[i]);
  745. }
  746. const titles = [];
  747. msgs.forEach((msg) => {
  748. let title = msg.querySelector('a.major').textContent;
  749. if (/^New update: /.test(title)) { // Select only adventure updates
  750. if (titles.indexOf(title) === -1) {
  751. if (msg.querySelector('td').style.cssText !== "border-left: 8px solid rgb(221, 221, 221);") {
  752. titles.push(title);
  753. }
  754. } else {
  755. msg.querySelector('input').click();
  756. }
  757. }
  758. });
  759. });
  760.  
  761. // Add buttons to the page.
  762. const del = document.querySelector('#deletemsgs');
  763. del.parentNode.appendChild(newBr());
  764. del.parentNode.appendChild(selRead);
  765. del.parentNode.appendChild(selDupe);
  766. }
  767. else if (location.pathname === "/my/messages/new/" && location.search) { // Auto-fill user when linked from a user page
  768. const recipientInput = document.querySelector('#addrecipient');
  769. recipientInput.value = location.search.replace('?u=', '');
  770. pageLoad(() => {
  771. const recipientButton = document.querySelector('#addrecipientbtn');
  772. if (recipientButton) {
  773. recipientButton.click();
  774. if (recipientInput.value === "") { // If the button press doesn't work
  775. return true;
  776. }
  777. }
  778. });
  779. }
  780. else if (location.pathname === "/my/stories/") {
  781. // Add links to buttons
  782. pageLoad(() => {
  783. const adventures = document.querySelectorAll('#stories tr');
  784. if (adventures.length > 0) {
  785. adventures.forEach(story => {
  786. const buttons = story.querySelectorAll('input.major');
  787. const id = story.querySelector('a').href.replace('https://mspfa.com/', '').replace('&p=1', '');
  788. if (id) {
  789. addLink(buttons[0], `/my/stories/info/${id}`);
  790. addLink(buttons[1], `/my/stories/pages/${id}`);
  791. }
  792. });
  793. return true;
  794. }
  795. });
  796.  
  797. // Add user guides
  798. const guides = ["A Guide To Uploading Your Comic To MSPFA", "MSPFA Etiquette", "Fanventure Guide for Dummies", "CSS Guide", "HTML and CSS Things", ];
  799. const links = ["https://docs.google.com/document/d/17QI6Cv_BMbr8l06RrRzysoRjASJ-ruWioEtVZfzvBzU/edit?usp=sharing", "/?s=27631", "/?s=29299", "/?s=21099", "/?s=23711"];
  800. const authors = ["Farfrom Tile", "Radical Dude 42", "nzar", "MadCreativity", "seymour schlong"];
  801.  
  802. const parentTd = document.querySelector('.container > tbody > tr:last-child > td');
  803. const unofficial = parentTd.querySelector('span');
  804. unofficial.textContent = "Unofficial Guides";
  805. const guideTable = document.createElement('table');
  806. const guideTbody = document.createElement('tbody');
  807. guideTable.style.width = "100%";
  808. guideTable.style.textAlign = "center";
  809.  
  810. guideTable.appendChild(guideTbody);
  811. parentTd.appendChild(guideTable);
  812.  
  813. for (let i = 0; i < guides.length; i++) {
  814. const guideTr = guideTbody.insertRow(i);
  815. const guideTd = guideTr.insertCell(0);
  816. const guideLink = document.createElement('a');
  817. guideLink.href = links[i];
  818. guideLink.textContent = guides[i];
  819. guideLink.className = "major";
  820. guideTd.appendChild(guideLink);
  821. guideTd.appendChild(newBr());
  822. guideTd.appendChild(document.createTextNode('by '+authors[i]));
  823. guideTd.appendChild(newBr());
  824. guideTd.appendChild(newBr());
  825. }
  826. }
  827. else if (location.pathname === "/my/stories/info/" && location.search) {
  828. // Button links
  829. addLink(document.querySelector('#userfavs'), `/readers/${location.search}`);
  830. addLink(document.querySelector('#editpages'), `/my/stories/pages/${location.search}`);
  831. }
  832. else if (location.pathname === "/my/stories/pages/" && location.search) {
  833. const adventureID = /\?s=(\d{5})/.exec(location.search)[1];
  834.  
  835. if (!settings.drafts[adventureID]) {
  836. settings.drafts[adventureID] = {}
  837. }
  838.  
  839. // Button links
  840. addLink(document.querySelector('#editinfo'), `/my/stories/info/?s=${adventureID}`);
  841.  
  842. // Default spoiler values
  843. const replaceButton = document.querySelector('#replaceall');
  844. const spoilerButton = document.createElement('input');
  845. spoilerButton.classList.add('major');
  846. spoilerButton.value = 'Default Spoiler Values';
  847. spoilerButton.type = 'button';
  848. replaceButton.parentNode.insertBefore(spoilerButton, replaceButton);
  849. replaceButton.parentNode.insertBefore(newBr(), replaceButton);
  850. replaceButton.parentNode.insertBefore(newBr(), replaceButton);
  851.  
  852. const spoilerSpan = document.createElement('span');
  853. const spoilerOpen = document.createElement('input');
  854. const spoilerClose = document.createElement('input');
  855. spoilerSpan.appendChild(document.createTextNode('Open button text:'));
  856. spoilerSpan.appendChild(newBr());
  857. spoilerSpan.appendChild(spoilerOpen);
  858. spoilerSpan.appendChild(newBr());
  859. spoilerSpan.appendChild(newBr());
  860. spoilerSpan.appendChild(document.createTextNode('Close button text:'));
  861. spoilerSpan.appendChild(newBr());
  862. spoilerSpan.appendChild(spoilerClose);
  863.  
  864. if (!settings.spoilerValues[adventureID]) {
  865. settings.spoilerValues[adventureID] = {
  866. open: 'Show',
  867. close: 'Hide'
  868. }
  869. }
  870.  
  871. spoilerOpen.value = settings.spoilerValues[adventureID].open;
  872. spoilerClose.value = settings.spoilerValues[adventureID].close;
  873.  
  874. spoilerButton.addEventListener('click', evt => {
  875. window.MSPFA.dialog('Default Spoiler Values', spoilerSpan, ['Save', 'Cancel'], (output, form) => {
  876. if (output === 'Save') {
  877. settings.spoilerValues[adventureID].open = spoilerOpen.value === '' ? 'Show' : spoilerOpen.value;
  878. settings.spoilerValues[adventureID].close = spoilerClose.value === '' ? 'Hide' : spoilerClose.value;
  879. if (settings.spoilerValues[adventureID].open === 'Show' && settings.spoilerValues[adventureID].close === 'Hide') {
  880. delete settings.spoilerValues[adventureID];
  881. }
  882. saveData(settings);
  883. }
  884. });
  885. });
  886.  
  887. document.querySelector('input[title="Spoiler"]').addEventListener('click', evt => {
  888. document.querySelector('#dialog input[name="open"]').value = settings.spoilerValues[adventureID].open;
  889. document.querySelector('#dialog input[name="close"]').value = settings.spoilerValues[adventureID].close;
  890. document.querySelector('#dialog input[name="open"]').placeholder = settings.spoilerValues[adventureID].open;
  891. document.querySelector('#dialog input[name="close"]').placeholder = settings.spoilerValues[adventureID].close;
  892. });
  893.  
  894. // Buttonless spoilers
  895. const flashButton = document.querySelector('input[title="Flash');
  896. const newSpoilerButton = document.createElement('input');
  897. newSpoilerButton.setAttribute('data-tag', 'Buttonless Spoiler');
  898. newSpoilerButton.title = 'Buttonless Spoiler';
  899. newSpoilerButton.type = 'button';
  900. 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");';
  901.  
  902. newSpoilerButton.addEventListener('click', evt => {
  903. const bbe = document.querySelector('#bbtoolbar').parentNode.querySelector('textarea');
  904. if (bbe) {
  905. bbe.focus();
  906. const start = bbe.selectionStart;
  907. const end = bbe.selectionEnd;
  908. bbe.value = bbe.value.slice(0, start) + '<div class="spoiler"><div>' + bbe.value.slice(start, end) + '</div></div>' + bbe.value.slice(end);
  909. bbe.selectionStart = start + 26;
  910. bbe.selectionEnd = end + 26;
  911. }
  912. });
  913.  
  914. flashButton.parentNode.insertBefore(newSpoilerButton, flashButton);
  915.  
  916. // Open preview in new tab with middle mouse
  917. document.body.addEventListener('mouseup', evt => {
  918. if (evt.toElement.value === "Preview" && evt.button === 1) {
  919. evt.toElement.click(); // TODO: Find a way to prevent the middle mouse scroll after clicking there.
  920. evt.preventDefault();
  921. return false;
  922. }
  923. });
  924.  
  925. // -- Drafts --
  926. // Accessing draft text
  927. const accessDraftsButton = document.createElement('input');
  928. accessDraftsButton.classList.add('major');
  929. accessDraftsButton.value = 'Saved Drafts';
  930. accessDraftsButton.type = 'button';
  931. replaceButton.parentNode.insertBefore(accessDraftsButton, replaceButton);
  932. accessDraftsButton.parentNode.insertBefore(newBr(), replaceButton);
  933. accessDraftsButton.parentNode.insertBefore(newBr(), replaceButton);
  934.  
  935. accessDraftsButton.addEventListener('click', () => {
  936. 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.');
  937. const draftInputTextarea = document.createElement('textarea');
  938. draftInputTextarea.placeholder = 'Paste your draft data here';
  939. draftInputTextarea.style = 'width: 100%; box-sizing: border-box; resize: vertical;';
  940. draftInputTextarea.rows = 8;
  941. draftDialog.appendChild(newBr());
  942. draftDialog.appendChild(newBr());
  943. draftDialog.appendChild(draftInputTextarea);
  944. setTimeout(() => {
  945. draftInputTextarea.focus();
  946. draftInputTextarea.selectionStart = 0;
  947. draftInputTextarea.selectionEnd = 0;
  948. draftInputTextarea.scrollTop = 0;
  949. }, 1);
  950.  
  951. draftInputTextarea.value = JSON.stringify(settings.drafts[adventureID], null, 4);
  952.  
  953. window.MSPFA.dialog('Saved Drafts', draftDialog, ['Load Draft', 'Cancel'], (output, form) => {
  954. if (output === "Load Draft") {
  955. if (draftInputTextarea.value === '') {
  956. setTimeout(() => {
  957. 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) => {
  958. if (output === "Delete") {
  959. settings.drafts[adventureID] = {};
  960. saveData(settings);
  961. }
  962. });
  963. }, 1);
  964. } else if (draftInputTextarea.value !== JSON.stringify(settings.drafts[adventureID], null, 4)) {
  965. setTimeout(() => {
  966. 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) => {
  967. if (output === "Load") {
  968. let newData = {};
  969. try { // Just in case the data given is invalid.
  970. newData = JSON.parse(draftInputTextarea.value);
  971. } catch (err) {
  972. console.error(err);
  973. setTimeout(() => {
  974. window.MSPFA.dialog('Error', window.MSPFA.parseBBCode('The entered data is invalid.'), ["Okay"]);
  975. }, 1);
  976. return;
  977. }
  978.  
  979. settings.drafts[adventureID] = newData;
  980. saveData(settings);
  981. }
  982. });
  983. }, 1);
  984. }
  985. }
  986. });
  987. });
  988.  
  989. // Draft stuff
  990. const msg = document.createElement('span');
  991. msg.appendChild(document.createTextNode('Command:'));
  992. msg.appendChild(document.createElement('br'));
  993.  
  994. const commandInput = document.createElement('input');
  995. commandInput.style = 'width: 100%; box-sizing: border-box;';
  996. commandInput.readOnly = true;
  997. commandInput.value = 'yes';
  998.  
  999. msg.appendChild(commandInput);
  1000. msg.appendChild(document.createElement('br'));
  1001. msg.appendChild(document.createElement('br'));
  1002.  
  1003. msg.appendChild(document.createTextNode('Body:'));
  1004.  
  1005. const bodyInput = document.createElement('textarea');
  1006. bodyInput.style = 'width: 100%; box-sizing: border-box; resize: vertical;';
  1007. bodyInput.readOnly = true;
  1008. bodyInput.rows = 8;
  1009. bodyInput.textContent = '';
  1010.  
  1011. msg.appendChild(bodyInput);
  1012.  
  1013. const showDraftDialog = (pageNum) => {
  1014. const pageElement = document.querySelector(`#p${pageNum}`);
  1015.  
  1016. let shownMessage = msg;
  1017. let optionButtons = [];
  1018.  
  1019. const commandElement = pageElement.querySelector('input[name="cmd"]');
  1020. const pageContentElement = pageElement.querySelector('textarea[name="body"]');
  1021.  
  1022. if (typeof settings.drafts[adventureID][pageNum] === "undefined") {
  1023. shownMessage = document.createTextNode('There is no draft saved for this page.');
  1024. optionButtons = ["Save New", "Close"];
  1025. } else {
  1026. commandInput.value = settings.drafts[adventureID][pageNum].command;
  1027. bodyInput.textContent = settings.drafts[adventureID][pageNum].pageContent;
  1028. optionButtons = ["Save New", "Load", "Delete", "Close"];
  1029. }
  1030.  
  1031. window.MSPFA.dialog(`Page ${pageNum} Draft`, shownMessage, optionButtons, (output, form) => {
  1032. if (output === "Save New") {
  1033. if (typeof settings.drafts[adventureID][pageNum] === "undefined") {
  1034. settings.drafts[adventureID][pageNum] = {
  1035. command: commandElement.value,
  1036. pageContent: pageContentElement.value
  1037. }
  1038. saveData(settings);
  1039. } else {
  1040. setTimeout(() => {
  1041. 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) => {
  1042. if (output === "Yes") {
  1043. settings.drafts[adventureID][pageNum] = {
  1044. command: commandElement.value,
  1045. pageContent: pageContentElement.value
  1046. }
  1047. saveData(settings);
  1048. }
  1049. });
  1050. }, 1);
  1051. }
  1052. } else if (output === "Load") {
  1053. if (pageContentElement.value === '' && (commandElement.value === '' || commandElement.value === document.querySelector('#defaultcmd').value)) {
  1054. commandElement.value = settings.drafts[adventureID][pageNum].command;
  1055. pageContentElement.value = settings.drafts[adventureID][pageNum].pageContent;
  1056. pageElement.querySelector('input[value="Save"]').disabled = false;
  1057. } else {
  1058. setTimeout(() => {
  1059. 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) => {
  1060. if (output === "Yes") {
  1061. commandElement.value = settings.drafts[adventureID][pageNum].command;
  1062. pageContentElement.value = settings.drafts[adventureID][pageNum].pageContent;
  1063. pageElement.querySelector('input[value="Save"]').disabled = false;
  1064. }
  1065. });
  1066. }, 1);
  1067. }
  1068. } else if (output === "Delete") {
  1069. setTimeout(() => {
  1070. window.MSPFA.dialog('Delete this draft?', document.createTextNode('This action is unreversable! Are you sure?'), ["Yes", "No"], (output, form) => {
  1071. if (output === "Yes") {
  1072. delete settings.drafts[adventureID][pageNum];
  1073. saveData(settings);
  1074. }
  1075. });
  1076. }, 1);
  1077. }
  1078. });
  1079. }
  1080.  
  1081. const createDraftButton = (form) => {
  1082. const draftButton = document.createElement('input');
  1083. draftButton.className = 'major draft';
  1084. draftButton.type = 'button';
  1085. draftButton.value = 'Draft';
  1086. draftButton.style = 'margin-right: 9.5px;';
  1087. draftButton.addEventListener('click', () => {
  1088. showDraftDialog(form.id.replace('p', ''));
  1089. });
  1090. return draftButton;
  1091. }
  1092.  
  1093. pageLoad(() => {
  1094. let allPages = document.querySelectorAll('#storypages form:not(#newpage)');
  1095. if (allPages.length !== 0) {
  1096. allPages.forEach(form => {
  1097. const prevButton = form.querySelector('input[name="preview"]');
  1098. prevButton.parentNode.insertBefore(createDraftButton(form), prevButton);
  1099. });
  1100. document.querySelector('input[value="Add"]').addEventListener('click', () => {
  1101. allPages = document.querySelectorAll('#storypages form:not(#newpage)');
  1102. const form = document.querySelector(`#p${allPages.length}`);
  1103. const prevButton = form.querySelector('input[name="preview"]');
  1104. prevButton.parentNode.insertBefore(createDraftButton(form), prevButton);
  1105. });
  1106. return true;
  1107. }
  1108. });
  1109.  
  1110. /* // Removed because apparently MSPFA already does this fine!
  1111. if (hashSearch) {
  1112. pageLoad(() => {
  1113. const element = document.querySelector(hashSearch);
  1114. if (element) {
  1115. if (element.style.display === "none") {
  1116. element.style = '';
  1117. }
  1118. return true;
  1119. }
  1120. });
  1121. }/**/
  1122. }
  1123. else if (location.pathname === "/user/") {
  1124. const id = location.search.slice(3);
  1125. const statAdd = [];
  1126. // Button links
  1127. pageLoad(() => {
  1128. const msgButton = document.querySelector('#sendmsg');
  1129. if (msgButton) {
  1130. addLink(msgButton, `/my/messages/new/${location.search}`);
  1131. addLink(document.querySelector('#favstories'), `/favs/${location.search}`);
  1132. return true;
  1133. }
  1134. });
  1135.  
  1136. // Add extra user stats
  1137. pageLoad(() => {
  1138. if (window.MSPFA) {
  1139. const stats = document.querySelector('#userinfo table');
  1140.  
  1141. const joinTr = stats.insertRow(1);
  1142. const joinTextTd = joinTr.insertCell(0);
  1143. joinTextTd.appendChild(document.createTextNode("Account created:"));
  1144. const joinDate = joinTr.insertCell(1);
  1145. const joinTime = document.createElement('b');
  1146. joinTime.textContent = "Loading...";
  1147. joinDate.appendChild(joinTime);
  1148.  
  1149. const advCountTr = stats.insertRow(2);
  1150. const advTextTd = advCountTr.insertCell(0);
  1151. advTextTd.appendChild(document.createTextNode("Adventures created:"));
  1152. const advCount = advCountTr.insertCell(1);
  1153. const advCountText = document.createElement('b');
  1154. advCountText.textContent = "Loading...";
  1155. advCount.appendChild(advCountText);
  1156.  
  1157. if (statAdd.indexOf('date') === -1) {
  1158. window.MSPFA.request(0, {
  1159. do: "user",
  1160. u: id
  1161. }, user => {
  1162. if (typeof user !== "undefined") {
  1163. statAdd.push('date');
  1164. joinTime.textContent = new Date(user.d).toString().split(' ').splice(1, 4).join(' ');
  1165. }
  1166. });
  1167. }
  1168.  
  1169. if (statAdd.indexOf('made') === -1) {
  1170. window.MSPFA.request(0, {
  1171. do: "editor",
  1172. u: id
  1173. }, s => {
  1174. if (typeof s !== "undefined") {
  1175. statAdd.push('made');
  1176. advCountText.textContent = s.length;
  1177. }
  1178. });
  1179. }
  1180.  
  1181. if (document.querySelector('#favstories').style.display !== 'none' && statAdd.indexOf('fav') === -1) {
  1182. statAdd.push('fav');
  1183. const favCountTr = stats.insertRow(3);
  1184. const favTextTd = favCountTr.insertCell(0);
  1185. favTextTd.appendChild(document.createTextNode("Adventures favorited:"));
  1186. const favCount = favCountTr.insertCell(1);
  1187. const favCountText = document.createElement('b');
  1188. favCountText.textContent = "Loading...";
  1189. window.MSPFA.request(0, {
  1190. do: "favs",
  1191. u: id
  1192. }, s => {
  1193. if (typeof s !== "undefined") {
  1194. favCountText.textContent = s.length;
  1195. }
  1196. });
  1197. favCount.appendChild(favCountText);
  1198. }
  1199.  
  1200. return true;
  1201. }
  1202. });
  1203. }
  1204. else if (location.pathname === "/favs/" && location.search) {
  1205. // Button links
  1206. pageLoad(() => {
  1207. const stories = document.querySelectorAll('#stories tr');
  1208. let favCount = 0;
  1209.  
  1210. if (stories.length > 0) {
  1211. stories.forEach(story => {
  1212. favCount++;
  1213. const id = story.querySelector('a').href.replace('https://mspfa.com/', '');
  1214. pageLoad(() => {
  1215. if (window.MSPFA.me.i) {
  1216. addLink(story.querySelector('.edit.major'), `/my/stories/info/${id}`);
  1217. return true;
  1218. }
  1219. });
  1220. addLink(story.querySelector('.rss.major'), `/rss/${id}`);
  1221. });
  1222.  
  1223. // Fav count
  1224. const username = document.querySelector('#username');
  1225. username.parentNode.appendChild(newBr());
  1226. username.parentNode.appendChild(newBr());
  1227. username.parentNode.appendChild(document.createTextNode(`Favorited adventures: ${favCount}`));
  1228.  
  1229. return true;
  1230. }
  1231. });
  1232. }
  1233. else if (location.pathname === "/search/" && location.search) {
  1234. // Character and word statistics
  1235. const statTable = document.createElement('table');
  1236. const statTbody = document.createElement('tbody');
  1237. const statTr = statTbody.insertRow(0);
  1238. const charCount = statTr.insertCell(0);
  1239. const wordCount = statTr.insertCell(0);
  1240. const statParentTr = document.querySelector('#pages').parentNode.parentNode.insertRow(2);
  1241. const statParentTd = statParentTr.insertCell(0);
  1242.  
  1243. const statHeaderTr = statTbody.insertRow(0);
  1244. const statHeader = document.createElement('th');
  1245. statHeader.colSpan = '2';
  1246.  
  1247. statHeaderTr.appendChild(statHeader);
  1248. statHeader.textContent = 'Statistics may not be entirely accurate.';
  1249.  
  1250. statTable.style.width = "100%";
  1251.  
  1252. charCount.textContent = "Character count: loading...";
  1253. wordCount.textContent = "Word count: loading...";
  1254.  
  1255. statTable.appendChild(statTbody);
  1256. statParentTd.appendChild(statTable);
  1257.  
  1258. pageLoad(() => {
  1259. if (document.querySelector('#pages br')) {
  1260. const bbc = window.MSPFA.BBC.slice();
  1261. bbc.splice(0, 3);
  1262.  
  1263. window.MSPFA.request(0, {
  1264. do: "story",
  1265. s: location.search.replace('?s=', '')
  1266. }, story => {
  1267. if (typeof story !== "undefined") {
  1268. const pageContent = [];
  1269. story.p.forEach(p => {
  1270. pageContent.push(p.c);
  1271. pageContent.push(p.b);
  1272. });
  1273.  
  1274. const storyText = pageContent.join(' ')
  1275. .replace(/\n/g, ' ')
  1276. .replace(bbc[0][0], '$1')
  1277. .replace(bbc[1][0], '$1')
  1278. .replace(bbc[2][0], '$1')
  1279. .replace(bbc[3][0], '$1')
  1280. .replace(bbc[4][0], '$2')
  1281. .replace(bbc[5][0], '$3')
  1282. .replace(bbc[6][0], '$3')
  1283. .replace(bbc[7][0], '$3')
  1284. .replace(bbc[8][0], '$3')
  1285. .replace(bbc[9][0], '$3')
  1286. .replace(bbc[10][0], '$2')
  1287. .replace(bbc[11][0], '$1')
  1288. .replace(bbc[12][0], '$3')
  1289. .replace(bbc[13][0], '$3')
  1290. .replace(bbc[14][0], '')
  1291. .replace(bbc[16][0], '$1')
  1292. .replace(bbc[17][0], '$2 $4 $5')
  1293. .replace(bbc[18][0], '$2 $4 $5')
  1294. .replace(bbc[19][0], '')
  1295. .replace(bbc[20][0], '')
  1296. .replace(/<(.*?)>/g, '');
  1297.  
  1298. wordCount.textContent = `Word count: ${storyText.split(/ +/g).length}`;
  1299. charCount.textContent = `Character count: ${storyText.replace(/ +/g, '').length}`;
  1300. }
  1301. });
  1302. return true;
  1303. }
  1304. });
  1305. }
  1306. })();