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.2
// @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/',
        'http://mail.xynu.edu.cn/',
    ];

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

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

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

        // 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 = '45px';
        button.style.zIndex = '9999';
        button.style.padding = '6px';
        button.style.backgroundColor = 'transparent';
        button.style.color = '#AAAAAA';
        button.style.border = '1px solid #DDDDDD';
        button.style.borderRadius = '4px';
        button.style.cursor = 'pointer';
        button.style.width = '80px';
        button.style.fontSize = '13px';
        button.style.fontFamily = 'Microsoft Yahei';
        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 = '70px';
        notification.style.width = '160px';
        notification.style.right = '10px';
        notification.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        notification.style.color = 'white';
        notification.style.padding = '8px';
        notification.style.borderRadius = '4px';
        notification.style.zIndex = '10000';
        notification.style.fontSize = '12px';
        notification.style.fontFamily = 'Microsoft Yahei';
        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 copy button (现在在share按钮的位置)
        let copyButton = createButton('Copy', '20px');

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

        // Create share button (现在在copy按钮的左侧)
        let shareButton = createButton('Share', '20px');
        shareButton.style.right = '135px'; // 将share按钮放在copy按钮的左侧

        // 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`);
        });

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