Bitbucket Copy `branchName`

Copy `branchName` into clipboard with Ctrl + Shift + C

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Bitbucket Copy `branchName`
// @namespace    http://bitbucket.org/copy/BranchName
// @version      1.1
// @description  Copy `branchName` into clipboard with Ctrl + Shift + C
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bitbucket.org
// @author       explainpark101
// @match        https://bitbucket.org/*
// @license      mit
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // Add CSS for the toast
    GM_addStyle(`
      #bb-toast {
        position: fixed;
        bottom: 20px;
        right: 20px;
        background: #323232;
        color: #fff;
        padding: 10px 20px;
        border-radius: 5px;
        opacity: 0;
        transition: opacity 0.3s ease, transform 0.3s ease;
        z-index: 9999;
        transform: translateY(20px);
      }
      #bb-toast.show {
        opacity: 1;
        transform: translateY(0);
      }
    `);

    // Create toast element
    const toast = document.createElement('div');
    toast.id = 'bb-toast';
    document.body.appendChild(toast);

    // Show toast function
    function showToast(message) {
        toast.textContent = message;
        toast.classList.add('show');
        setTimeout(() => {
            toast.classList.remove('show');
        }, 2000);
    }

    // Function to get `at` param value
    function getAtParam() {
        const params = new URLSearchParams(window.location.search);
        return params.get('at');
    }

    // Listen for Ctrl + Shift + C
    document.addEventListener('keydown', function(e) {
        if (e.ctrlKey && e.shiftKey && e.code === 'KeyC') {
            e.preventDefault();
            if (location.pathname.split('/').at(3) == "src") {
                const atValue = getAtParam();
                if (atValue) {
                    navigator.clipboard.writeText(atValue).then(() => {
                        showToast(`Branch Name Copied: ${atValue}`);
                    }).catch(err => {
                        showToast('Failed to copy.');
                        console.error(err);
                    });
                }
                else if(location.pathname.split('/').at(4)) {
                    const atValue = location.pathname.split('/').at(4);
                    if (atValue)
                        navigator.clipboard.writeText(atValue).then(() => {
                            showToast(`Branch Name Copied: ${atValue}`);
                        }).catch(err => {
                            showToast('Failed to copy.');
                            console.error(err);
                        });
                    else showToast('No branchName found.');
                }
                else {
                    showToast('No `at` parameter found.');
                }
            }
            if (location.pathname.split('/').at(3) == "pull-requests") {
                const atValue = getAtParam();
                if (atValue) {
                    navigator.clipboard.writeText(atValue).then(() => {
                        showToast(`Branch Name Copied: ${atValue}`);
                    }).catch(err => {
                        showToast('Failed to copy.');
                        console.error(err);
                    });
                }
                else {
                    showToast('No `at` parameter found.');
                }
            }
            else if (location.pathname.split('/').at(3) == "branch") {
                const atValue = location.pathname.split('/').slice(4).join('/');
                if (atValue) {
                    navigator.clipboard.writeText(atValue).then(() => {
                        showToast(`Branch Name Copied: ${atValue}`);
                    }).catch(err => {
                        showToast('Failed to copy.');
                        console.error(err);
                    });
                } else {
                    showToast('No `branchName` found.');
                }
            }
            else if (location.pathname.split('/').at(3) == "commits" && location.pathname.split('/').at(4) == "branch") {
                const atValue = location.pathname.split('/').slice(5).join('/');
                if (atValue) {
                    navigator.clipboard.writeText(atValue).then(() => {
                        showToast(`Branch Name Copied: ${atValue}`);
                    }).catch(err => {
                        showToast('Failed to copy.');
                        console.error(err);
                    });
                } else {
                    showToast('No `branchName` found.');
                }
            }
            else {
                if (location.pathname.split("/").at(3) == "overview") return showToast("Please enter into repository first.");
                showToast("No branchName Found");
            }
        }
    });
})();