Greasy Fork 还支持 简体中文。

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