Lute: Random Book Button

Add random book button to Lute

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Lute: Random Book Button
// @version      20240223
// @description  Add random book button to Lute
// @author       jamesdeluk
// @match        http://localhost:500*
// @grant        none
// @namespace https://greasyfork.org/users/242246
// ==/UserScript==

(function() {
    'use strict';

    // Create the button
    var button = document.createElement('button');
    button.innerHTML = 'Random book';
    button.style.marginLeft = '10px';
    button.style.padding = '0.1em 0.3em';

    // Add an event listener to the button
    button.addEventListener('click', function() {
        var e = document.getElementsByClassName("book-title");
        var t = [];
        for (var n = 0; n < e.length; n++) {
            t.push(e[n].getAttribute("href"));
        }
        var r = Math.floor(Math.random() * t.length);
        var o = t[r];
        var currentUrl = window.location.origin;
        var newUrl = currentUrl + o;
        window.location.href = newUrl;
    });

    // Insert the button before the "booktable" element
    var booktable = document.getElementById('booktable');
    if (booktable) {
        booktable.parentNode.insertBefore(button, booktable);
    }
})();