Greasy Fork is available in English.

hover-copy

Hover over A or IMG, then press Ctrl+c to copy link.

  1. // ==UserScript==
  2. // @name hover-copy
  3. // @namespace https://github.com/nameldk/user-script
  4. // @version 0.1
  5. // @description Hover over A or IMG, then press Ctrl+c to copy link.
  6. // @author You
  7. // @match https://*/*
  8. // @match http://*/*
  9. // @require https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // copy from: https://chrome.google.com/webstore/detail/hover-and-copy/moanghbdjdafhoppikcalbkggpnbhhid
  17. $(document).ready(function () {
  18. var $body = $('body');
  19.  
  20. function isUrlValid(url) {
  21. var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
  22. return regexp.test(url) || /^data:image\//.test(url);
  23. }
  24.  
  25. var inputForCopy = $('<input id="input_for_copy">');
  26.  
  27. $body.append(inputForCopy);
  28.  
  29. inputForCopy.css({
  30. position: 'absolute',
  31. left: -9999
  32. });
  33.  
  34. $body.on('mouseenter', 'a, img', function (e) {
  35. var hoverTarget = $(e.target);
  36. var hoverLink;
  37.  
  38. if (hoverTarget.is('img')) {
  39. hoverLink = hoverTarget.attr('src');
  40. } else {
  41. hoverLink = hoverTarget.attr('href');
  42. if (typeof hoverLink === 'undefined') {
  43. hoverLink = hoverTarget.closest('a').attr('href');
  44. }
  45. }
  46.  
  47. if (!isUrlValid(hoverLink)) {
  48. hoverLink = window.location.origin + '/' + hoverLink;
  49. }
  50.  
  51. inputForCopy.focus({
  52. preventScroll: true,
  53. });
  54.  
  55. inputForCopy.val(hoverLink);
  56. inputForCopy.select();
  57.  
  58. }).on('mouseleave', 'a, img', function () {
  59. inputForCopy.blur();
  60. })
  61. });
  62. })();