Search code examples
javascriptjquerygreasemonkeytampermonkey

How to copy href / link with Tampermonkey


I am doing copy a link with tampermonkey beta and am using chrome
so how can i copy a link in " " symbol i try many other method i failed </li><li class="item" data-id="805" data-url="http://www???/listen/805/aa.mp3" data1-url="http://??/??.mp3" data-url="http://??/song/??/">

// @name         ???/
// @version      0.3.1
// @match        *://???/*
// @grant        GM_setValue
// @grant        GM_download
// @require    https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @grant        GM_openInTab
// ==/UserScript==

'use strict';
/* global $ */
(function() {
var at1 = $("a:contains('/listen/')");
if (at1.length) {
   GM_openInTab(at1[0].href);
}
})();```


Solution

  • Here is an example of what you can start with...

    There is no need to use 700+ functions of Jquery for such a small code.

    If HTML is like this:

    </li><li class="item" data-id="805" data-url="http://www..../listen/805/aa.mp3" 
    data1-url="http://.../....mp3" data-url2="http://..../song/??/">....</li>
    

    Supposing there is only 1 link that you want to open and the data-url ends with ".mp3"

    // @name         Open MP3
    // @version      1.0
    // @match        *://*/*
    // @grant        GM_openInTab
    // ==/UserScript==
    
    
    const link = document.querySelector('li[data-url$=".mp3"]');
    if (link) {
      GM_openInTab(link.getAttribute('data-url'));
    }