GGn Corner Button

Add buttons to group content's top corners. In the main script, @grant GM_getValue to be customise position in settings. @grant GM_registerMenuCommand to register a Run command if position is set to none

目前為 2025-07-04 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/541342/1618982/GGn%20Corner%20Button.js

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         GGn Corner Button
// @version      2
// @description  Add buttons to group content's top corners. In the main script, @grant GM_getValue to be customise position in settings. @grant GM_registerMenuCommand to register a Run command if position is set to none
// @author       ingts
// @match        https://gazellegames.net/
// ==/UserScript==
/**
 * @typedef {('none' | 'left' | 'left-vertical' | 'right' | 'right-vertical')} Position
 */

/**
 * @type HTMLDivElement
 */
const groupContentDiv = document.getElementById('content')

/**
 * @param {Position} position
 * @param {string} text
 * @param {(e: MouseEvent) => void} onclick
 */
function createCornerButton(position, text, onclick) {
    if (!groupContentDiv) return

    if (typeof GM_getValue !== "undefined") {
        if (typeof GM_getValue('corner_button') === 'undefined')
            GM_setValue('corner_button', position)

        position = GM_getValue('corner_button')
    }

    if (typeof GM_registerMenuCommand !== "undefined" && position === 'none') {
        GM_registerMenuCommand('Run', onclick)
        return
    }

    const container = createContainer(position)

    const button = document.createElement('button')
    button.textContent = text
    button.type = 'button'
    if (position.includes('vertical')) {
        button.style.writingMode = 'vertical-lr'
        button.style.height = 'unset'

        if (position.includes('left')) {
            button.style.rotate = 'z 180deg'
        }
    }
    button.style.padding = '2px'
    button.onclick = e => onclick(e)
    container.append(button)

    let leftPos = 0
    let topPos = 0
    switch (position) {
        case 'left':
            leftPos = groupContentDiv.offsetLeft + container.offsetWidth - container.scrollWidth
            topPos = groupContentDiv.offsetTop - container.offsetHeight
            break
        case 'left-vertical':
            leftPos = groupContentDiv.offsetLeft - container.offsetWidth
            topPos = groupContentDiv.offsetTop
            break
        case 'right':
            leftPos = groupContentDiv.offsetLeft + groupContentDiv.offsetWidth - container.scrollWidth
            topPos = groupContentDiv.offsetTop - container.offsetHeight
            break
        case 'right-vertical':
            leftPos = groupContentDiv.offsetLeft + groupContentDiv.offsetWidth
            topPos = groupContentDiv.offsetTop
    }

    requestAnimationFrame(() => {
        container.style.left = leftPos + 'px'
        container.style.top = topPos + 'px'
    })
}

/**
 * @private
 * @param {Position} position
 */
function createContainer(position) {
    const elementId = `${position}-corner-container`
    let container = document.getElementById(elementId)
    if (container) return container

    container = document.createElement('div')
    container.id = elementId
    container.style.position = 'absolute'
    container.style.display = 'flex'
    if (position.includes('vertical')) {
        container.style.flexDirection = 'column'
    }
    container.style.gap = '2px'

    document.body.append(container)
    return container
}