Discussions » Demandes de création de scripts

Help: An Userscript to automatically copy urls on a page while loading

§
Posté le: 13/02/2017
Édité le: 13/02/2017

Help: An Userscript to automatically copy urls on a page while loading

Hi, I was trying to compose a script which will automatically copy all links pointing to https://serverhost.net/mylink/display?link=*

Illustration: Lets assume we are browsing http://mypage.com/index.php This page contains an html segment:

< h2 >< a href = "https://serverhost.net/mylink/display?link=By6HjlZfyo4JtBcF2rRumQuifAiJfKg7cti" target="_blank" >Download Button< /a >< /h2 >

I want to copy all links that are pointing to https://serverhost.net/mylink/display?link=[variable string] available on this page.

I tried to use GM_setClipboard, but its not quite working for me. Please help.

Thanks.

woxxomMod
§
Posté le: 13/02/2017

Something like this:

// ==UserScript==//
// @name      Copy links
// @match     http://mypage.com/index.php*
// @grant     GM_setClipboard
// ==/UserScript==

GM_setClipboard(
    [].map.call(
        document.querySelectorAll('a[href*="https://serverhost.net/mylink/display?link="]'),
        e => e.href
    ).join('\n')
);

However the real problem might be the site updating its pages dynamically after the userscript has been injected (so-called AJAX-site). In that case either wrap the code in setTimeout or use a sliding timer in a MutationObserver callback.

§
Posté le: 14/02/2017

Thank you very much sir.
But, I feel the 'match' function should be replaced by something else, because there are some more pages apart from 'mypage.com' where it should be working.

Thanks again.

woxxomMod
§
Posté le: 14/02/2017

re: @match, you can declare it for as many sites as needed, see the docs

// @match https://www.example.com/*
// @match http://*.example.com/*

Poster une réponse

Connectez-vous pour poster une réponse.