0

Similar to this question, but not limited to <a...> link elements; instead I want to copy the title attribute from an arbitrary element (usually a TD in my use case) to the clipboard.

I would be content either with a context menu choice, or a hotkey. Either way, the element that the mouse is directly on top of is the one I want to grab the title from.

In my use case, the site Digicert abbreviates "long" strings with an ellipsis and puts the whole string in a title. (Instead of using CSS, which can do this for any string without needing to change the string, they actually emit
<td title="very long string" class="text-xs-left">very lon...</td>
I've told them there's a better way, but it remains this clunky way.)

1 Answer 1

1

you can use tampermonkey chrome extension to do that,
here is script:

// ==UserScript==
// @name         ctrl + mouse-right-click to copy title from cloest element
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://*/*
// @grant        GM_setClipboard
// ==/UserScript==

;
(function() {
    'use strict';

let clickFn=(ev)=>{
    console.log(`begin copy title from cloest element ...`)
    if(ev.ctrlKey){
        ev.preventDefault()
    }else{
        console.log(` please hold down the Ctrl key`)
        return
    }
    let target=ev.target
    let v=""

    if(1){
        let pp=target
        let bb=document.body
        let distance=-1
        let attr='title'
        // attr='id'
        while(pp!==bb){
            distance++
            if(pp.hasAttribute(attr)){
                v=pp.getAttribute(attr)
                console.log(`dis:${distance}|tag:${pp.tagName}|title:${v}`)
                GM_setClipboard(v,"text")
                break
            }
            pp=pp.parentElement
        }
    }
    if(v){
        console.log(`copy title success`)
    }else{
        console.log(`title Not found`)
    }
}

//document.addEventListener("click",clickFn)
document.addEventListener("contextmenu",clickFn)

})();

usage:

hold the ctrl key down,and press the mouse right-click key

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .