Discussions » Creation Requests

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

§
Posted: 2017/02/13
Edited: 2017/02/13

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
§
Posted: 2017/02/13

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.

§
Posted: 2017/02/14

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
§
Posted: 2017/02/14

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

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

Post reply

Sign in to post a reply.