skip to Main Content

Apologies for asking this question again as per here and here there are answers.
BUT

This appears to be specific to a page. I am building a simple "button" for the browser bookmark bar that will grab the Name and URL or the page I am on to paste into a spreadsheet easily.
On other sites, I use Javascript to find the text and then I can easily paste anywhere.

example from another site (business.currys.co.uk)

javascript:void(
eval('function copyToClipboard(element) {    
var $temp = $("<input>");    
$("body").append($temp);    
$temp.val(element).select();    
document.execCommand("copy");
    $temp.remove();}
    var copyableVal =  $("#product-container > h1").text().trim() + " " + $(".page-title span:last-child").text() + " " + $("#product-container > div.product-info > form > dl > dd:nth-child(2)").text().trim();
copyToClipboard(copyableVal);'))

I want to do that with LinkedIn. To repeat – open any linkedin Profile page.
The Name is in this element

<h1 class="text-heading-xlarge inline t-24 v-align-middle break-words">Jannes Sörensen</h1>

<!-- Full Xpath -->
/html/body/div[6]/div[3]/div/div/div[2]/div/div/main/section[1]/div[2]/div[2]/div[1]/div[1]/span[1]/a/h1

I am testing the uniqueness of the classes with

$( document ).ready(function() {
  var classCount= $('.t-24').length;
  console.log(classCount);
 });

$( document ).ready(function() {
  var classCount = $(':header').length;
  console.log(classCount);
 });

However I keep getting 0 in the console 🙁
I wonder if Jquery is blocked somehow?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the direction mPlungjan. I have taken your answer as inspiration and created some "plain javascript" to make it work.

    With some help from perplexity.ai too.

    I have created this "grab" - save it as a bookmark and by clicking it, you will get a clipboard entry that is: Person Name, Position, URL Then you can save people you are connecting with easily for later reference or importing to other CRMs.

    javascript:(function(){
        var lnData = "";
        lnData += document.getElementsByClassName("t-24")[0].innerText;
        lnData += "%09";
        lnData += document.getElementsByClassName("text-body-medium")[0].innerText;
        lnData += "%09";
        lnData += document.location.href;
        navigator.clipboard.writeText(lnData).then(function() {
            alert("Copied to clipboard");
        }).catch(function(err) {
            console.error('Failed to copy: ', err);
        });
    })();
    

  2. You need to click it to get it.

    Here is a plain js version which does not rely on jQuery

    You need to check the selectors. I for example do not find a product-container on a linkedin profile page

    javascript: (() => {
      const copyToClipboard = (text) => {
        let temp = document.createElement("input");
        temp.value = text;
        document.body.appendChild(temp);
        temp.addEventListener('click',(e) => {
          let tgt = e.target;
          tgt.focus();
          tgt.select();
          document.execCommand("copy");
          //temp.remove();
        });  
      };
      let container = document.getElementById('product-container'),
        copyableVal = `${container.querySelector('h1').textContent.trim()} ${document.querySelector(".page-title span:last-child").textContent.trim()} ${container.querySelector('div.product-info > form > dl > dd:nth-child(2)').textContent.trim()}`;
        console.log(copyableVal)
      copyToClipboard(copyableVal);
    })()
    <div id="product-container">
      <h1>Header</h1>
      <div class="page-title">
        <span>1</span><span>2</span><span>last span</span>
      </div>
      <div class="product-info">
        <form>
          <dl>
            <dd>DD1</dd>
            <dd>DD2</dd>
            <dd>DD3</dd>
            <dd>DD4</dd>
          </dl>
        </form>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search