A
A
Alexey2019-04-10 09:20:22
Browsers
Alexey, 2019-04-10 09:20:22

"Hotkeys" for copying link url?

You need to quickly copy the addresses of links in the text. How can this be done with a combination of "hot keys"? Do not offer the option with the right mouse button -)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Taratin, 2019-04-10
@Taraflex

var x = null;
var y = null;
    
document.addEventListener('mousemove', onMouseUpdate, false);
document.addEventListener('mouseenter', onMouseUpdate, false);
    
function onMouseUpdate(e) {
  x = e.clientX;
  y = e.clientY;
}


function contains(x,y, rect){
  return rect.left <= x && x <= rect.right && rect.top <= y && y <= rect.bottom;
}

hotkeys('ctrl+d',function(event){

    for(let link of Array.prototype.slice.call(document.getElementsByTagName('a')) ){
      if(contains(x, y, link.getBoundingClientRect())){
        event.preventDefault();
        event.stopImmediatePropagation();
       
        if(link.href){
          console.log(link.href);
          navigator.clipboard.writeText(link.href);
        }
       
        break;
      }
    }
})

https://jsfiddle.net/QW01_01/j3ydxp28/26/
Libraries used
https://github.com/lgarron/clipboard-polyfill
https://github.com/jaywcjlove/hotkeys

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question