WaniKani API

Tools for accessing the WaniKani API without having to use jQuery.

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/34539/226222/WaniKani%20API.js

  1. // ==UserScript==
  2. // @name WaniKani API
  3. // @version 1.0
  4. // @description Tools for accessing the WaniKani API without having to use jQuery.
  5. // @copyright 2017 jeshuam
  6. // @grant GM_getValue
  7. // @grant GM_setValue
  8. // @grant GM_registerMenuCommand
  9. // ==/UserScript==
  10.  
  11. /**
  12. * WaniKani API utilities, for use within userscripts.
  13. * Just place this script in the @require section of the userscript header.
  14. */
  15. (function() {
  16. // Common key to use for the API key.
  17. var API_RETREIVAL_KEY = 'jeshuam-wanikani-apikey';
  18.  
  19. var WaniKaniAPI = window.WaniKaniAPI = {
  20. /**
  21. * Get the API key from storage and return it. If it doesn't exist, return null.
  22. */
  23. getAPIKey: function() {
  24. return GM_getValue(API_RETREIVAL_KEY, undefined);
  25. },
  26.  
  27. /**
  28. * Insert the API key into storage. If you are deleting, rather use deleteAPIKey().
  29. */
  30. setAPIKey: function(apiKey) {
  31. GM_setValue(API_RETREIVAL_KEY, apiKey);
  32. },
  33.  
  34. /**
  35. * Remove the API key from storage.
  36. */
  37. deleteAPIKey: function() {
  38. GM_deleteValue(API_RETREIVAL_KEY);
  39. },
  40.  
  41. /**
  42. * Get the API base URL.
  43. */
  44. apiURL: function(action) {
  45. return 'https://www.wanikani.com/api/v1.2/user/' + WaniKaniAPI.getAPIKey() + '/' + action;
  46. },
  47.  
  48. /**
  49. * Make an AJAX request to the given API url, and call `callback` when finished.
  50. */
  51. load: function(url, callback) {
  52. var xhr;
  53.  
  54. // Get the XHR element first.
  55. if (typeof XMLHttpRequest !== 'undefined') {
  56. xhr = new XMLHttpRequest();
  57. } else {
  58. var versions = ["MSXML2.XmlHttp.5.0",
  59. "MSXML2.XmlHttp.4.0",
  60. "MSXML2.XmlHttp.3.0",
  61. "MSXML2.XmlHttp.2.0",
  62. "Microsoft.XmlHttp"
  63. ]
  64.  
  65. for (var i = 0, len = versions.length; i < len; i++) {
  66. try {
  67. xhr = new ActiveXObject(versions[i]);
  68. break;
  69. } catch (e) {
  70.  
  71. }
  72. }
  73. }
  74.  
  75. // Function to execute when the state of the XHR request changes.
  76. xhr.onreadystatechange = function() {
  77. if (xhr.readyState < 4) {
  78. return;
  79. }
  80.  
  81. if (xhr.status !== 200) {
  82. return;
  83. }
  84.  
  85. if (xhr.readyState === 4) {
  86. callback(JSON.parse(xhr.responseText));
  87. }
  88. };
  89.  
  90. // Start the request.
  91. xhr.open('GET', url, true);
  92. xhr.send('');
  93. }
  94. };
  95.  
  96. // Register some GreaseMonkey commands.
  97. GM_registerMenuCommand('JeshuaM Scripts: Change API Key', function() {
  98. var apiKey = prompt('Please enter your API key.', WaniKaniAPI.getAPIKey() || '');
  99. if (apiKey !== null) {
  100. WaniKaniAPI.setAPIKey(apiKey);
  101. alert('JeshuaM Scripts: API Key Saved! ' + apiKey);
  102. }
  103. });
  104.  
  105. GM_registerMenuCommand('JeshuaM Scripts: Reset API Key', function() {
  106. WaniKaniAPI.deleteAPIKey();
  107. alert('JeshuaM Scripts: API Key Deleted!');
  108. });
  109. })();
  110.  
  111. //////////////////////////////
  112. /////// Start Function ///////
  113. //////////////////////////////
  114. document.addEventListener('DOMContentLoaded', function() {
  115. if (WaniKaniAPI.getAPIKey() === undefined) {
  116. // If we are on the account page, populate the API key (and maybe move back
  117. // to where we were before).
  118. if (window.location.href.indexOf('settings/account') >= 0) {
  119. key = document.querySelector('#user_api_key').value;
  120. WaniKaniAPI.setAPIKey(key);
  121.  
  122. // From http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
  123. let getParameterByName = function(name) {
  124. name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  125. var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
  126. var results = regex.exec(location.search);
  127. return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
  128. };
  129.  
  130. // Notify the user, then redirect if necessary.
  131. var redirect = getParameterByName('prev');
  132. if (redirect) {
  133. window.alert('WANIKANI-API: API key set to ' + key + '! Going back to ' + redirect);
  134. window.location.href = redirect;
  135. } else {
  136. window.alert('WANIKANI-API: API key set to ' + key + '!');
  137. }
  138. } else {
  139. if (window.confirm('WANIKANI-API: Moving to settings page to fetch API key!')) {
  140. window.location.href = '/settings/account?prev=' + window.location.href;
  141. }
  142. }
  143. }
  144. });