convenient for code write

获得当前网页元素上的所有的链接


  • Method 1 (short)

    参考

    1
    2
    3
    [...new Set(Array.from(document.querySelectorAll('[src],[href]')).map(i=>i.src||i.href))]
    [...new Set(Array.from($$('[src],[href]')).map(i=>i.src||i.href))]
    // ...剥衣;new Set去重;$$=document.querySelectorAll
  • human-readable

    1
    2
    3
    4
    5
    6
    7
    8
    9
    let urls = []
    $$('*').forEach(i => {
    urls.push(i.src)
    urls.push(i.href)
    urls.push(i.url)
    });
    console.log(...new Set(urls))

    //(*) means all elements
  • human-readable( with regex )

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    let urls = []
    $$('*').forEach(i => {
    urls.push(i.src)
    urls.push(i.href)
    urls.push(i.url)
    });
    regexUrls= $.grep(urls, function (n) {
    try {return n.match(/.*http://XXX.COM.*/i)} catch (e) {}
    }); console.log(...new Set(regexUrls))

    // http://XXX.COM. change to your search target
⬆︎UP