Add to Shaarli

按下 Ctrl+5 将当前网页添加到 Shaarli(v2.1), 首次使用需填写一次域名(如: http://www.domain.com:8000),更改域名请可按下Ctrl+Shift+5。

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Add to Shaarli
// @namespace    http://tampermonkey.net/
// @version      2025-09-28
// @description  按下 Ctrl+5 将当前网页添加到 Shaarli(v2.1), 首次使用需填写一次域名(如: http://www.domain.com:8000),更改域名请可按下Ctrl+Shift+5。
// @author       Leo Bi
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        GM_setValue
// @grant        GM_getValue
// @require      https://code.jquery.com/jquery-2.1.4.min.js
// ==/UserScript==

(function ($) {
    'use strict';

    const STORAGE_KEY = 'shaarli_domain';

    /* ---------- 域名存取 ---------- */
    function getDomain() {
        return GM_getValue(STORAGE_KEY);
    }
    function saveDomain(raw) {
        let d = raw.trim().replace(/\/+$/, '');
        if (!/^https?:\/\//i.test(d)) d = 'http://' + d;
        GM_setValue(STORAGE_KEY, d);
        return d;
    }

    /* ---------- 弹窗填写 ---------- */
    function promptDomain() {
        const domain = prompt(
            '首次使用 Add to Shaarli\n请输入 Shaarli 域名\n(例如:http://www.domain.com:8000)',
            ''
        );
        return (domain && domain.trim()) ? saveDomain(domain) : null;
    }

    /* ---------- 打开分享页 ---------- */
    function openShaarli() {
        let domain = getDomain();
        if (!domain) {
            domain = promptDomain();
            if (!domain) return;          // 用户取消
        }
        const shareURL = domain + '/admin/shaare?post=' + encodeURIComponent(location.href);
        window.open(shareURL, '_blank');
    }

    /* ---------- 快捷键 ---------- */
    $(document).on('keydown', function (e) {
        if (e.ctrlKey && e.keyCode === 53 && !e.shiftKey) {        // Ctrl+5
            e.preventDefault();
            openShaarli();
        }
        if (e.ctrlKey && e.shiftKey && e.keyCode === 53) {        // Ctrl+Shift+5
            e.preventDefault();
            const d = promptDomain();
            if (d) alert('Shaarli 域名已更新为:' + d);
        }
    });
})(jQuery);