Clean Copy URL

Clean and copy the URL by pressing "alt+c"

目前為 2023-10-01 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Clean Copy URL
// @namespace
// @version      0.0.0
// @description  Clean and copy the URL by pressing "alt+c"
// @author       Piotr Stojanow (https://github.com/psto/)
// @license      MIT
// @homepageURL  https://github.com/psto/userscript-clean-copy-url
// @supportURL   https://github.com/psto/userscript-clean-copy-url
// @icon         data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🔗</text></svg>
// @match        *://*/*
// @grant        none
// @namespace https://greasyfork.org/users/287072
// ==/UserScript==

(function() {
  'use strict'

  document.addEventListener('keydown', (event) => {
    // Check if the alt and c keys are pressed
    if (event.altKey && event.code === 'KeyC') {
      // Get the current URL
      let url = window.location.href
      // Find the index of the question mark
      const index = url.indexOf('?')
      // If there is a question mark, delete everything after it
      if (index !== -1) {
        url = url.slice(0, index)
        // Replace the current URL with the new one
        window.history.replaceState(null, null, url)
        // Copy the new URL to the clipboard
        navigator.clipboard.writeText(url)
          .then(() => {
            // Go to the new URL
            window.location.href = url
          })
          .catch(() => {
            throw new Error('copying failed')
          })
        // Otherwise just copy the URL
      }
      else {
        navigator.clipboard.writeText(url)
      }
    }
  })
})()