WK Move a Lesson To Review

Selectively move a Lesson to Review

当前为 2023-11-06 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         WK Move a Lesson To Review
// @namespace    wanikani
// @version      0.1.1
// @description  Selectively move a Lesson to Review
// @author       polv
// @match        *://www.wanikani.com/*
// @match        *://preview.wanikani.com/*
// @license      MIT
// @grant        GM_xmlhttpRequest
// @require      https://greasyfork.org/scripts/430565-wanikani-item-info-injector/code/WaniKani%20Item%20Info%20Injector.user.js?version=1241826
// @icon         https://www.google.com/s2/favicons?sz=64&domain=wanikani.com
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  const USE_API_BUTTON = true;

  const modules = 'ItemData,Settings';

  wkof.include(modules);

  let r = {};
  const injector = wkItemInfo
    .on('itemPage')
    .appendAtTop('Move to Review', (o) => {
      if (o.id !== r.id) {
        r = o;
        wkof
          .ready(modules)
          .then(() =>
            wkof.Settings.load('wklcp', {
              apikey: localStorage.getItem('apiv2_key') || 'none',
            }),
          )
          .then(() =>
            wkof.ItemData.get_items('assignments').then((rs) => {
              r = rs.find((r) => r.id === o.id) || r;
              injector.renew();
            }),
          );
      }

      console.log(r.assignments);
      if (!r.assignments) return;
      if (r.assignments.unlocked_at && !r.assignments.available_at) {
        if (USE_API_BUTTON) {
          const button = document.createElement('button');
          button.type = 'button';
          button.onclick = () => {
            send_learn_request(r.id);
          };
          button.innerText = 'Send to Review';
          return button;
        }

        const a = document.createElement('a');
        a.target = '_blank';
        a.href = `https://www.wanikani.com/subjects/lesson/quiz?queue=${o.id}`;
        a.innerText = 'Lesson Quiz';
        return a;
      }
    });

  async function send_learn_request(ass_id) {
    return new Promise((resolve, reject) => {
      // GM_ évite problème CORS
      GM_xmlhttpRequest({
        method: 'PUT',
        url: 'https://api.wanikani.com/v2/assignments/' + ass_id + '/start',
        headers: {
          Authorization: 'Bearer ' + wkof.settings.wklcp.apikey,
          'Wanikani-Revision': '20170710',
        },
        onload: function (response) {
          console.log(JSON.parse(response.responseText));
          if (response.status != 200) {
            if (
              confirm(
                'WK API answered : ' +
                  response.status +
                  ' ' +
                  response.statusText +
                  '\nDo you want to enter a different API key?',
              )
            ) {
              add_key();
              return resolve(false);
            }
          }
          location.reload();
          return resolve(true);
        },
        onerror: reject,
      });
    });
  }

  function add_key() {
    var dirtykey = prompt(
      "Please enter an API key with 'assignment start' permission",
    );
    if (dirtykey != null) {
      wkof.settings.wklcp.apikey = dirtykey;
      wkof.Settings.save('wklcp');
    }
  }
})();