Share to Weibo

Share webpage title and link to Weibo

目前為 2024-10-03 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Share to Weibo
// @namespace    http://tampermonkey.net/
// @version      0.1
// @icon         https://weibo.com/favicon.ico
// @description  Share webpage title and link to Weibo
// @author       Jing Wang
// @contact      [email protected]
// @license      GPL-3.0
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to process the title
    function processTitle(title) {
        // Replace "- cnBeta.COM 移动版(WAP)" with "- cnBeta"
        title = title.replace("- cnBeta.COM 移动版(WAP)", "- cnBeta");

        // Remove "(X+ 封私信 / Y 条消息)" pattern, where X and Y can be any number
        title = title.replace(/\(\d+\+?\s*封私信\s*\/\s*\d+\s*条消息\)/g, "");

        return title.trim(); // Trim any leading or trailing whitespace
    }

    // Create share button
    let shareButton = document.createElement('button');
    shareButton.textContent = '分享到微博'; // "Share to Weibo" in Chinese
    shareButton.style.position = 'fixed';
    shareButton.style.bottom = '110px';
    shareButton.style.right = '55px';
    shareButton.style.zIndex = '9999';
    shareButton.style.padding = '10px';
    shareButton.style.backgroundColor = '#FF8200'; // Weibo orange
    shareButton.style.color = 'white';
    shareButton.style.border = 'none';
    shareButton.style.borderRadius = '5px';
    shareButton.style.cursor = 'pointer';

    // Add click event listener
    shareButton.addEventListener('click', function() {
        let title = processTitle(document.title);
        let url = window.location.href;
        let shareText = encodeURIComponent(title);
        let weiboUrl = `https://service.weibo.com/share/share.php?url=${encodeURIComponent(url)}&title=${shareText}`;
        window.open(weiboUrl, '_blank');
    });

    // Add button to the page
    document.body.appendChild(shareButton);
})();