Selectively move a Lesson to Review
当前为
// ==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');
}
}
})();