Copy or Share

Copy webpage title and URL to clipboard or share to Fanfou

// ==UserScript==
// @name         Copy or Share
// @namespace    http://tampermonkey.net/
// @version      2.1
// @icon         http://static.fanfou.com/favicon.ico
// @description  Copy webpage title and URL to clipboard or share to Fanfou
// @author       Jing Wang
// @contact      [email protected]
// @license      GPL-3.0
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // List of websites to exclude (blacklist)
    const blacklistedSites = [
        'https://fanfou.com/sharer/image',
        'https://co.gocheck.cn/'
    ];

    // Check if the current page is in the blacklist
    function isBlacklistedSite() {
        return blacklistedSites.some(site => window.location.href.startsWith(site));
    }

    // 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
    }

    // Function to create a button with common styles
    function createButton(text, bottom) {
        let button = document.createElement('button');
        button.textContent = text;
        button.style.position = 'fixed';
        button.style.bottom = bottom;
        button.style.right = '55px';
        button.style.zIndex = '9999';
        button.style.padding = '10px';
        button.style.backgroundColor = 'transparent';
        button.style.color = '#777777'; // 更浅的灰色
        button.style.border = '1px solid #CCCCCC';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';
        button.style.width = '100px';
        button.style.fontWeight = 'bold';
        button.style.fontFamily = 'Helvetica, Arial, sans-serif'; // 添加 Helvetica 字体
        return button;
    }

    // Function to copy text to clipboard
    function copyToClipboard(text) {
        // Create a temporary textarea element
        const textarea = document.createElement('textarea');
        textarea.value = text;
        textarea.style.position = 'fixed';  // Prevent scrolling to bottom
        document.body.appendChild(textarea);
        textarea.select();

        try {
            // Execute copy command
            const successful = document.execCommand('copy');
            const msg = successful ? 'Copied to clipboard' : 'Copy failed';
            console.log(msg);

            // Show a temporary notification
            showNotification(msg);
        } catch (err) {
            console.error('Copy failed:', err);
            showNotification('Copy failed');
        }

        // Clean up
        document.body.removeChild(textarea);
    }

    // Function to show a temporary notification
    function showNotification(message) {
        const notification = document.createElement('div');
        notification.textContent = message;
        notification.style.position = 'fixed';
        notification.style.bottom = '140px'; // 从170px减小到140px
        notification.style.width = '140px';
        notification.style.right = '35px';
        notification.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        notification.style.color = 'white';
        notification.style.padding = '10px';
        notification.style.borderRadius = '5px';
        notification.style.zIndex = '10000';
        notification.style.fontSize = '12px';
        notification.style.fontFamily = 'Helvetica, Arial, sans-serif';
        notification.style.textAlign = 'center'; // 文本居中
        notification.style.boxSizing = 'border-box'; // 确保宽度计算包含内边距

        document.body.appendChild(notification);

        // Remove notification after 2 seconds
        setTimeout(() => {
            document.body.removeChild(notification);
        }, 2000);
    }

    // Only run the script if the current site is not blacklisted
    if (!isBlacklistedSite()) {
        // Create share button
        let shareButton = createButton('Share', '50px'); // 从45px增加到50px

        // Add click event listener for share button
        shareButton.addEventListener('click', function() {
            let title = processTitle(document.title);
            let url = window.location.href;
            let shareText = encodeURIComponent(title);
            let fanfouUrl = `https://fanfou.com/sharer/image?u=${encodeURIComponent(url)}&t=${shareText}`;

            // Calculate window size and position
            let width = 670;
            let height = 350;
            let left = (window.screen.width - width) / 2;
            let top = (window.screen.height - height) / 2;

            // Open a popup window without address bar and toolbar, centered on the screen
            window.open(fanfouUrl, 'fanfou_share', `width=${width},height=${height},left=${left},top=${top},location=no,menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no`);
        });

        // Create copy button
        let copyButton = createButton('Copy', '95px'); // 从110px减小到95px

        // Add click event listener for copy button
        copyButton.addEventListener('click', function() {
            let title = processTitle(document.title);
            let url = window.location.href;
            let copyText = `${title}\n${url}`;

            copyToClipboard(copyText);
        });

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