WK Move a Lesson To Review

Selectively move a Lesson to Review

目前為 2023-11-06 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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');
    }
  }
})();