Redirect YouTube to WaniKani 18

Redirect YouTube to WaniKani if there are reviews available, prompt for API token if not set, recheck every hour, and optionally check for lessons on second visit

当前为 2024-08-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         Redirect YouTube to WaniKani 18
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Redirect YouTube to WaniKani if there are reviews available, prompt for API token if not set, recheck every hour, and optionally check for lessons on second visit
// @author       Your Name
// @match        https://www.youtube.com/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-start
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to check WaniKani API
    function checkWaniKani() {
        // Get the API token from storage
        let apiToken = GM_getValue('wanikaniApiToken');
        let checkLessons = GM_getValue('checkLessons', null); // Use null to check if it's the first visit

        // If the API token is not set, prompt the user for it
        if (!apiToken) {
            apiToken = prompt('Please enter your WaniKani API token:');
            if (apiToken) {
                GM_setValue('wanikaniApiToken', apiToken);
                // Force to prompt for lesson checking next time
                GM_setValue('checkLessons', null);
            } else {
                console.error('No API token provided.');
                return;
            }
        }

        // If checkLessons preference is not set, ask the user if they want to enable lesson checking
        if (checkLessons === null) {
            if (confirm('Would you like to activate lesson checking as well?')) {
                GM_setValue('checkLessons', true);
            } else {
                GM_setValue('checkLessons', false);
            }
            // Exit early to avoid making the API call on the first visit
            return;
        }

        // Prepare API URL
        let apiUrl = 'https://api.wanikani.com/v2/assignments?immediately_available_for_review=true';
        if (checkLessons) {
            apiUrl = 'https://api.wanikani.com/v2/assignments?immediately_available_for_lessons=true';
        }

        console.log('API URL:', apiUrl); // Log the API URL for debugging

        // Make an API request to WaniKani to check the number of available reviews and lessons
        GM_xmlhttpRequest({
            method: 'GET',
            url: apiUrl,
            headers: {
                'Authorization': `Bearer ${apiToken}`,
                'Wanikani-Revision': '20170710'
            },
            onload: function(response) {
                try {
                    const data = JSON.parse(response.responseText);
                    const totalCount = data.total_count;

                    console.log('Total Count:', totalCount); // Log the total count for debugging

                    // Check if total count is greater than 0
                    if (totalCount > 0) {
                        // Redirect to WaniKani
                        window.location.href = 'https://www.wanikani.com';
                    }
                } catch (error) {
                    console.error('Error parsing API response:', error);
                }
            },
            onerror: function(error) {
                console.error('Error fetching data:', error);
            }
        });
    }

    // Run the check initially
    checkWaniKani();

    // Set an interval to check every hour (3,600,000 milliseconds)
    setInterval(checkWaniKani, 3600000);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址