Clean Copy URL

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

  1. // ==UserScript==
  2. // @name Clean Copy URL
  3. // @namespace https://stojanow.com/
  4. // @version 0.2.0
  5. // @description Clean and copy the URL by pressing "alt+c"
  6. // @author Piotr Stojanow (https://github.com/psto/)
  7. // @license MIT
  8. // @homepageURL https://github.com/psto/userscript-clean-copy-url
  9. // @supportURL https://github.com/psto/userscript-clean-copy-url
  10. // @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>
  11. // @match *://*/*
  12. // @grant GM_notification
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. 'use strict'
  17.  
  18. document.addEventListener('keydown', async (event) => {
  19. // Check if the alt and c keys are pressed
  20. if (event.altKey && event.code === 'KeyC') {
  21. // Get the current URL
  22. let url = window.location.href
  23. // Find the index of the question mark
  24. const index = url.indexOf('?')
  25. // If there is a question mark, delete everything after it
  26. if (index !== -1) {
  27. url = url.slice(0, index)
  28. // Replace the current URL with the new one
  29. window.history.replaceState(null, null, url)
  30. try {
  31. // Copy the new URL to the clipboard
  32. await navigator.clipboard.writeText(url)
  33. GM_notification(`Copied ${url}`, 'Clean Copy URL', '', null)
  34. // Go to the new URL
  35. window.location.href = url
  36. }
  37. catch (error) {
  38. GM_notification('Failed to copy URL', 'Clean Copy URL', '', null)
  39. console.error(error)
  40. }
  41. // Otherwise just copy the URL
  42. }
  43. else {
  44. try {
  45. await navigator.clipboard.writeText(url)
  46. GM_notification(`Copied ${url}`, 'Clean Copy URL', '', null)
  47. }
  48. catch (error) {
  49. GM_notification('Failed to copy URL', 'Clean Copy URL', '', null)
  50. console.error(error)
  51. }
  52. }
  53. }
  54. })
  55. })()