JPDB-Export

Allows you to export your JPDB decks (see readme on github for more info)

  1. // ==UserScript==
  2. // @name JPDB-Export
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1.6
  5. // @description Allows you to export your JPDB decks (see readme on github for more info)
  6. // @author JaiWWW
  7. // @license GPL-3.0
  8. // @match https://jpdb.io/deck?*
  9. // @match https://jpdb.io/add-to-deck-from-shirabe-jisho*
  10. // @icon https://www.google.com/s2/favicons?sz=64&domain=jpdb.io
  11. // @homepageURL https://github.com/JaiWWW/JPDB-Export
  12. // @supportURL https://github.com/JaiWWW/JPDB-Export/issues/new
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // ==/UserScript==
  16.  
  17. /*
  18.  
  19. Changelog:
  20. 1. Fixed a bug with export button creation in decks that were already pinned.
  21.  
  22. */
  23.  
  24. let debug = false; // Set true to enter debug mode
  25. // Any line beginning with "debug &&" will only run if this is set to true
  26. let superdebug = false; // Creates way more console logs
  27. // Any line beginning with "superdebug &&" will only run if this is set to true
  28.  
  29.  
  30. // Start of required script: FileSaver.js by eligrey
  31. // https://github.com/eligrey/FileSaver.js
  32.  
  33. (function (global, factory) {
  34. if (typeof define === "function" && define.amd) {
  35. define([], factory);
  36. } else if (typeof exports !== "undefined") {
  37. factory();
  38. } else {
  39. var mod = {
  40. exports: {}
  41. };
  42. factory();
  43. global.FileSaver = mod.exports;
  44. }
  45. })(this, function () {
  46. "use strict";
  47.  
  48. /*
  49. * FileSaver.js
  50. * A saveAs() FileSaver implementation.
  51. *
  52. * By Eli Grey, http://eligrey.com
  53. *
  54. * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
  55. * source : http://purl.eligrey.com/github/FileSaver.js
  56. */
  57. // The one and only way of getting global scope in all environments
  58. // https://stackoverflow.com/q/3277182/1008999
  59. var _global = typeof window === 'object' && window.window === window ? window : typeof self === 'object' && self.self === self ? self : typeof global === 'object' && global.global === global ? global : void 0;
  60.  
  61. function bom(blob, opts) {
  62. if (typeof opts === 'undefined') opts = {
  63. autoBom: false
  64. };else if (typeof opts !== 'object') {
  65. console.warn('Deprecated: Expected third argument to be a object');
  66. opts = {
  67. autoBom: !opts
  68. };
  69. } // prepend BOM for UTF-8 XML and text/* types (including HTML)
  70. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  71.  
  72. if (opts.autoBom && /^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  73. return new Blob([String.fromCharCode(0xFEFF), blob], {
  74. type: blob.type
  75. });
  76. }
  77.  
  78. return blob;
  79. }
  80.  
  81. function download(url, name, opts) {
  82. var xhr = new XMLHttpRequest();
  83. xhr.open('GET', url);
  84. xhr.responseType = 'blob';
  85.  
  86. xhr.onload = function () {
  87. saveAs(xhr.response, name, opts);
  88. };
  89.  
  90. xhr.onerror = function () {
  91. console.error('could not download file');
  92. };
  93.  
  94. xhr.send();
  95. }
  96.  
  97. function corsEnabled(url) {
  98. var xhr = new XMLHttpRequest(); // use sync to avoid popup blocker
  99.  
  100. xhr.open('HEAD', url, false);
  101.  
  102. try {
  103. xhr.send();
  104. } catch (e) {}
  105.  
  106. return xhr.status >= 200 && xhr.status <= 299;
  107. } // `a.click()` doesn't work for all browsers (#465)
  108.  
  109.  
  110. function click(node) {
  111. try {
  112. node.dispatchEvent(new MouseEvent('click'));
  113. } catch (e) {
  114. var evt = document.createEvent('MouseEvents');
  115. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
  116. node.dispatchEvent(evt);
  117. }
  118. } // Detect WebView inside a native macOS app by ruling out all browsers
  119. // We just need to check for 'Safari' because all other browsers (besides Firefox) include that too
  120. // https://www.whatismybrowser.com/guides/the-latest-user-agent/macos
  121.  
  122.  
  123. var isMacOSWebView = /Macintosh/.test(navigator.userAgent) && /AppleWebKit/.test(navigator.userAgent) && !/Safari/.test(navigator.userAgent);
  124. var saveAs = _global.saveAs || ( // probably in some web worker
  125. typeof window !== 'object' || window !== _global ? function saveAs() {}
  126. /* noop */
  127. // Use download attribute first if possible (#193 Lumia mobile) unless this is a macOS WebView
  128. : 'download' in HTMLAnchorElement.prototype && !isMacOSWebView ? function saveAs(blob, name, opts) {
  129. var URL = _global.URL || _global.webkitURL;
  130. var a = document.createElement('a');
  131. name = name || blob.name || 'download';
  132. a.download = name;
  133. a.rel = 'noopener'; // tabnabbing
  134. // TODO: detect chrome extensions & packaged apps
  135. // a.target = '_blank'
  136.  
  137. if (typeof blob === 'string') {
  138. // Support regular links
  139. a.href = blob;
  140.  
  141. if (a.origin !== location.origin) {
  142. corsEnabled(a.href) ? download(blob, name, opts) : click(a, a.target = '_blank');
  143. } else {
  144. click(a);
  145. }
  146. } else {
  147. // Support blobs
  148. a.href = URL.createObjectURL(blob);
  149. setTimeout(function () {
  150. URL.revokeObjectURL(a.href);
  151. }, 4E4); // 40s
  152.  
  153. setTimeout(function () {
  154. click(a);
  155. }, 0);
  156. }
  157. } // Use msSaveOrOpenBlob as a second approach
  158. : 'msSaveOrOpenBlob' in navigator ? function saveAs(blob, name, opts) {
  159. name = name || blob.name || 'download';
  160.  
  161. if (typeof blob === 'string') {
  162. if (corsEnabled(blob)) {
  163. download(blob, name, opts);
  164. } else {
  165. var a = document.createElement('a');
  166. a.href = blob;
  167. a.target = '_blank';
  168. setTimeout(function () {
  169. click(a);
  170. });
  171. }
  172. } else {
  173. navigator.msSaveOrOpenBlob(bom(blob, opts), name);
  174. }
  175. } // Fallback to using FileReader and a popup
  176. : function saveAs(blob, name, opts, popup) {
  177. // Open a popup immediately do go around popup blocker
  178. // Mostly only available on user interaction and the fileReader is async so...
  179. popup = popup || open('', '_blank');
  180.  
  181. if (popup) {
  182. popup.document.title = popup.document.body.innerText = 'downloading...';
  183. }
  184.  
  185. if (typeof blob === 'string') return download(blob, name, opts);
  186. var force = blob.type === 'application/octet-stream';
  187.  
  188. var isSafari = /constructor/i.test(_global.HTMLElement) || _global.safari;
  189.  
  190. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  191.  
  192. if ((isChromeIOS || force && isSafari || isMacOSWebView) && typeof FileReader !== 'undefined') {
  193. // Safari doesn't allow downloading of blob URLs
  194. var reader = new FileReader();
  195.  
  196. reader.onloadend = function () {
  197. var url = reader.result;
  198. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
  199. if (popup) popup.location.href = url;else location = url;
  200. popup = null; // reverse-tabnabbing #460
  201. };
  202.  
  203. reader.readAsDataURL(blob);
  204. } else {
  205. var URL = _global.URL || _global.webkitURL;
  206. var url = URL.createObjectURL(blob);
  207. if (popup) popup.location = url;else location.href = url;
  208. popup = null; // reverse-tabnabbing #460
  209.  
  210. setTimeout(function () {
  211. URL.revokeObjectURL(url);
  212. }, 4E4); // 40s
  213. }
  214. });
  215. _global.saveAs = saveAs.saveAs = saveAs;
  216.  
  217. if (typeof module !== 'undefined') {
  218. module.exports = saveAs;
  219. }
  220. });
  221.  
  222. // End of required script: FileSaver.js by eligrey
  223. // https://github.com/eligrey/FileSaver.js
  224.  
  225.  
  226.  
  227. (function() {
  228. 'use strict';
  229.  
  230. if (!GM_getValue('debugPass') && (debug || superdebug)) { // If debug and/or super debug mode is enabled AND debugPass is falsy
  231. if (window.confirm("Looks like you have debug mode enabled. Are you sure you want to continue?")) {
  232. if (superdebug) { // If super debug mode is enabled
  233. if (window.confirm("Are you really sure you want to continue in super debug mode? This will drown your console!")) {
  234. debug = true; // Just in case you only turned on superdebug - how naughty!
  235. console.log("JPDB-Export successfully launched with super debug mode enabled.");
  236. } else {
  237. superdebug = false;
  238. console.log("JPDB-Export successfully launched with debug mode enabled.");
  239. }
  240. } else {
  241. console.log("JPDB-Export successfully launched with debug mode enabled.");
  242. }
  243. } else {
  244. debug = false;
  245. superdebug = false;
  246. }
  247. }
  248.  
  249. const URL = window.location.href;
  250. debug && console.log(`URL = ${URL}`);
  251.  
  252. if (URL.startsWith('https://jpdb.io/deck')) { // Deck page
  253.  
  254. debug && console.log("Deck page detected");
  255.  
  256. let workingURL = GM_getValue('workingURL'); // if export is in progress, workingURL will store the URL of the next page to be exported
  257. debug && console.log(`workingURL = ${workingURL}`);
  258.  
  259. function exportToCSV() { // Export the deck contents to a CSV file
  260.  
  261. debug && console.log("Called exportToCSV()");
  262. debug && GM_setValue('debugPass', true) // Skip debug check until export is finished
  263.  
  264. // Test if FileSaver.js is supported
  265. let supported;
  266. try {
  267. const isFileSaverSupported = !!new Blob;
  268. // isFileSaverSupported = 1; // Uncomment to test what happens on unsupported browsers
  269. debug && console.log("Download script supported");
  270. supported = true;
  271. } catch (e) {
  272. debug && console.log("Download script not supported");
  273. supported = false;
  274. const errorMessage = 'Userscript "JPDB-Export":\n\nSorry, your browser does not support the system used to download files. Please see this link for more information: https://github.com/eligrey/FileSaver.js#user-content-supported-browsers\n\nDo you want to go to this link now? (opens in new tab)';
  275. if (confirm(errorMessage)) { // If they click OK to go to the link
  276. window.open("https://github.com/eligrey/FileSaver.js#user-content-supported-browsers");
  277. }
  278. }
  279.  
  280. if (supported) {
  281.  
  282. debug && console.log("Script passed supported check, now running simultaneous export check");
  283.  
  284. workingURL = GM_getValue('workingURL'); // Refreshing workingURL so that the alreadyExporting check works without a refresh
  285. debug && console.log(`workingURL = ${workingURL}`);
  286. const alreadyExporting = "Sorry, it seems like you already have an export in progress somewhere else. This script does not currently support simultaneous exports.\n\nIf this is an error, please report it on github by pressing the bug icon in your userscript manager's panel or dashboard. Thanks!";
  287. if (workingURL && URL != workingURL) { // workingURL active and on a different URL - i.e. an export is in progress somewhere else
  288. debug && console.log("Simultaneous export detected");
  289. return window.alert(alreadyExporting);
  290. }
  291.  
  292. const confirmationMessage = "Exporting your deck may take some time if it has a lot of pages. Continue?";
  293. if ((URL === workingURL) || confirm(confirmationMessage)) { // If they are already in progress or click OK to start export
  294.  
  295. debug && console.log("Export confirmed. Attempting to start export");
  296.  
  297. function addPageToFile() { // Append the current page to the file
  298. debug && console.log("Called addPageToFile()");
  299.  
  300. const vocabList = document.querySelector("body > div.container.bugfix > div.vocabulary-list"); // Div containing all the vocab on the page
  301. debug && console.log("vocabList:", vocabList);
  302.  
  303. let wordWrapper; // The <a> tag surrounding each word
  304. let stringK; // This will store the current kanji string being found to add to the CSV
  305. let stringR; // This will store the current reading string being found to add to the CSV
  306. let fileContents = GM_getValue('fileContents'); // Get the current file contents into fileContents
  307. debug && console.log("File is currently", fileContents.split("\n").length-1, "lines long.");
  308.  
  309. debug && console.log("Looping through each element in the vocab list:");
  310. for (let i = 1; i <= vocabList.childElementCount; i++) { // Loop through each word in the vocab list
  311.  
  312. superdebug && console.log(`Word number ${i}`);
  313. stringK = '';
  314. stringR = '';
  315. wordWrapper = vocabList.querySelector(`div:nth-child(${i}) > div:nth-child(1) > div.vocabulary-spelling > a`);
  316. superdebug && console.log("wordWrapper:", wordWrapper);
  317. for (let j = 0; j < wordWrapper.childElementCount; j++) { // Loop through each ruby element in this word
  318. superdebug && console.log("Checking:", wordWrapper.children[j]);
  319. if (wordWrapper.children[j].childElementCount === 0) { // If this ruby element has no children (i.e. it's kana)
  320. superdebug && console.log("It's kana, adding to both strings");
  321. // Add the contents to both strings
  322. stringR += wordWrapper.children[j].textContent;
  323. stringK += wordWrapper.children[j].textContent;
  324. } else { // If this ruby element is kanji
  325. superdebug && console.log("It's kanji, adding kanji to stringK and furigana to stringR");
  326. stringK += wordWrapper.children[j].firstChild.textContent; // Add the kanji to the kanji string
  327. stringR += wordWrapper.children[j].children[0].textContent; // Add the furigana to the reading string
  328. }
  329. }
  330. superdebug && console.log(`Adding the following line to fileContents: "${stringK},${stringR}," plus a line break`);
  331. fileContents += `${stringK},${stringR},\n`; // Append the correctly formatted strings to fileContents
  332. }
  333. debug && console.log("Adding page to 'fileContents'");
  334. GM_setValue('fileContents', fileContents); // Update the file contents
  335. }
  336.  
  337. function createFileName() { // Create a file name and upload to storage
  338. debug && console.log("Called createFileName()");
  339.  
  340. const container = document.querySelector("body > div.container.bugfix");
  341. debug && console.log("container:", container);
  342. const deckName = container.firstChild.nextSibling.textContent;
  343. superdebug && console.log(`deckName = ${deckName}`);
  344. const current = new Date();
  345. const time = current.toLocaleTimeString();
  346. const date = current.toLocaleDateString();
  347. const fileName = `_${deckName}_ deck export at ${time} on ${date}.csv`;
  348. debug && console.log(`filename = ${fileName}, returning fileName`);
  349. return fileName;
  350. }
  351.  
  352. function downloadFile() { // Download the file
  353. debug && console.log("Called downloadFile()");
  354.  
  355. superdebug && console.log("Attempting call createFileName()");
  356. const fileName = createFileName();
  357. superdebug && console.log("Getting file contents");
  358. const fileContents = GM_getValue('fileContents');
  359.  
  360. const blob = new Blob([fileContents], {type: "text/plain;charset=utf-8"});
  361. debug && console.log("Saving file");
  362. saveAs(blob, fileName);
  363. GM_setValue('workingURL', ''); // Clear working URL
  364. GM_setValue('debugPass', false) // Clear debug pass
  365. }
  366.  
  367. function lastPage() { // Test if we are on the last page or not
  368. debug && console.log("Called lastPage()");
  369.  
  370. const pagination = document.querySelector("body > div.container.bugfix > div.pagination"); // Div that shows "Next page"
  371. debug && pagination ? console.log("pagination:", pagination): console.log("Single-page deck detected");
  372. if (!pagination || pagination.textContent.indexOf("Next page") < 0) { // Last page
  373. debug && console.log("Last page detected");
  374. return true;
  375. } else { // More pages to go
  376. debug && console.log("More pages detected");
  377. return false;
  378. }
  379. }
  380.  
  381. if (URL.indexOf("offset=") < 0) { // If they are on the first page of the deck
  382.  
  383. debug && console.log("First page detected.");
  384.  
  385. GM_setValue('fileContents', ''); // Initiate fileContents
  386. superdebug && console.log("fileContents initiated.");
  387. superdebug && console.log("Attempting to call addPageToFile()");
  388. addPageToFile();
  389.  
  390. superdebug && console.log("Attempting to call lastPage()");
  391. if (lastPage()) { // Download file
  392. superdebug && console.log("Attempting to call downloadFile()");
  393. downloadFile();
  394. } else { // Redirect to next page
  395.  
  396. // URL looks like 'https://jpdb.io/deck?id=123' potentially with '#a' at the end
  397. const redirect = URL.replace('#a', '') + '&offset=50';
  398. debug && console.log(`Redirecting to ${redirect}`);
  399. GM_setValue('workingURL', redirect);
  400. window.location.replace(redirect);
  401. }
  402.  
  403. } else { // They are not on the first page
  404. debug && console.log("Non-first page detected");
  405.  
  406. if (URL === workingURL) { // If export is already in progress and they are on the right page
  407.  
  408. superdebug && console.log("Attempting to call addPage()");
  409. addPageToFile();
  410.  
  411. superdebug && console.log("Attempting to call lastPage()");
  412. if (lastPage()) { // Download file
  413. superdebug && console.log("Attempting to call downloadFile()");
  414. downloadFile();
  415. } else { // Redirect to next page
  416.  
  417. // URL looks like 'https://jpdb.io/deck=123&offset=200'
  418. const offsetIndex = URL.indexOf("offset=") + 7; // First character of the actual offset number
  419. const offset = parseInt(URL.slice(offsetIndex)) + 50; // Offset value of next page
  420. superdebug && console.log(`Next offset = ${offset}`);
  421.  
  422. const redirect = URL.slice(0, offsetIndex) + offset;
  423. debug && console.log(`Redirecting to ${redirect}`);
  424. GM_setValue('workingURL', redirect)
  425. window.location.replace(redirect);
  426. }
  427.  
  428.  
  429. } else { // User wants to start export but has to go to the first page
  430.  
  431. const firstPage = URL.slice(0,URL.indexOf("offset=")-1);
  432.  
  433. debug && console.log(`Redirecting to ${firstPage}`);
  434. GM_setValue('workingURL', firstPage);
  435. window.location.replace(firstPage); // Go to first page
  436.  
  437. }
  438. }
  439. }
  440. }
  441. }
  442.  
  443. if (URL === workingURL) {
  444. debug && console.log("URL matches working URL");
  445. exportToCSV();
  446. } else { // Don't bother tweaking the page if we're alredy in progress
  447. const menu = document.querySelector("body > div.container.bugfix > div.dropdown > details > div").firstChild; // UL of options in the menu
  448. debug && console.log("menu:", menu);
  449. let shirabe;
  450.  
  451. for (let i=0; i<menu.childElementCount; i++) { // Loop through looking for import button
  452. if (menu.children[i].firstChild.lastChild.value === "Import from Shirabe Jisho") { // Found import button
  453. shirabe = menu.children[i];
  454. debug && console.log("Found shirabe:", shirabe);
  455. break;
  456. }
  457. }
  458. shirabe.firstChild.lastChild.setAttribute('value', 'Import from CSV'); // Rename to "Import from CSV"
  459. debug && console.log("Renamed import button");
  460.  
  461. // Add "Export to CSV" button
  462. shirabe.insertAdjacentHTML('afterend', '<li id="export"><form class="link-like" method="dialog"><input type="submit" value="Export to CSV"></form></li>');
  463. debug && console.log("Added export button");
  464.  
  465. const exportButton = document.getElementById("export");
  466. exportButton.addEventListener('click', exportToCSV); // Call exportToCSV() when exportButton is clicked
  467. debug && console.log("Added event listener to export button");
  468. }
  469. }
  470.  
  471. if (URL.startsWith('https://jpdb.io/add-to')) { // Import page
  472.  
  473. debug && console.log("Import page detected");
  474.  
  475. const heading = document.querySelector("body > div.container.bugfix > h4"); // "Import from Shirabe Jisho" heading
  476. debug && console.log("heading:", heading);
  477. heading.innerHTML = 'Import from CSV'; // Changing the heading
  478. debug && console.log("Changed heading");
  479.  
  480. const bulletOne = document.querySelector("body > div.container.bugfix > ul > li:nth-child(1)"); // First bullet point
  481. debug && console.log("bulletOne:", bulletOne);
  482. bulletOne.innerHTML += ', decks or any other correctly-formatted CSV file';
  483. debug && console.log("Edited first bullet point");
  484. // Add a bullet point explaining how to find the correct format
  485. bulletOne.insertAdjacentHTML(
  486. 'afterend', '<li>To see an example of the correct format, try exporting a deck and opening the file in a text editor</li>');
  487. debug && console.log("Inserted a new bullet point");
  488. }
  489. })();