아브자막 다운로더

아브자막 다운로더 도우미

目前为 2022-06-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         아브자막 다운로더
// @namespace    아브자막 다운로더
// @version      1.3
// @description  아브자막 다운로더 도우미
// @author       DandyClubs
// @include      /avjamak\.(net|com)/
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
// @connect      *
// @grant		 GM_xmlhttpRequest
// @grant        window.close
// @grant		 GM_addStyle
// @grant		 GM_openInTab
// @grant        unsafeWindow
// @license      MIT
// @run-at       document-end
// ==/UserScript==

(function() { var css = document.createElement('link'); css.href = 'https://use.fontawesome.com/releases/v5.15.1/css/all.css'; css.rel = 'stylesheet'; css.type = 'text/css'; document.getElementsByTagName('head')[0].appendChild(css); })();

GM_addStyle (`

.OpenIcon {
    cursor: pointer;
    right: auto;
    left: 95%;
    font-size: 1.5rem;
}

.NextPage {
    position: relative !important;
    cursor: pointer;
    color: dodgerblue !important;
    font-size: 1.5rem;
}
`);

const PageURL = window.location !== window.parent.location ? document.referrer : document.location.href;


Start()

var filename, i, LinkUrl


async function Start(){

    if(!/wr_id/.test(PageURL) && /jamakbos|jamakuser|bigsub|jamak_sujung|jamakfreer/.test(PageURL)){
        document.querySelector('.panel-heading a').insertAdjacentHTML('beforeend', '<div class="OpenIcon fas fa-external-link-alt"></>')
        document.querySelector('.OpenIcon').insertAdjacentHTML('beforeend', '&nbsp;&nbsp;<div class="NextPage fas fa-arrow-circle-right"></>')
        //document.querySelector('.OpenIcon').style.setProperty("top", '10px' )
        document.querySelector('.OpenIcon').style.setProperty('color', 'dodgerblue', 'important');
        document.querySelector('.OpenIcon').style.setProperty('position', 'absolute')
        document.querySelector('.OpenIcon').addEventListener("click", function(event) {
            event.preventDefault()
            event.stopPropagation()
            event.stopImmediatePropagation()
            OpenLinks(LinkDB)
        })
        document.querySelector('div.NextPage').addEventListener("click", function(event) {
            event.preventDefault()
            event.stopPropagation()
            event.stopImmediatePropagation()
            var pageNumber = document.location.href.match(/(.*page=)(\d+)/) ? Number(document.location.href.match(/(.*page=)(\d+)/)[2]) + 1 : '&page=2'
            var PreURL = document.location.href.match(/(.*page=)(\d+)/) ? document.location.href.match(/(.*page=)(\d+)/)[1] : document.location.href
            window.location = PreURL + pageNumber
        })


        var Linksitems = document.querySelectorAll('div.list-container > div.list-row > div.list-item > h2 > a')
        let filtered = [...Linksitems].filter(n => n.textContent.length >= 4)
        var LinkDB = []
        if(/jamakbos|jamakuser|bigsub/.test(PageURL)){
            for (i = 0; i < filtered.length; i++) {
                var last = [...filtered[i].closest('div.list-item').querySelectorAll('.list-details.text-muted .pull-right')].pop()
                MP = last && last.textContent.match(/(\d+)/) ? Number(last.textContent.match(/(\d+)/).pop()) : ''
                if(!MP || MP <=1){
                    //console.log(filtered[i])
                    LinkDB.push(filtered[i])
                }
            }
            console.log(LinkDB)
        }
        else {
            LinkDB = Linksitems
        }
    }
    else if(/wr_id/.test(PageURL) && /jamakbos|jamakuser|bigsub|jamak_sujung|jamakfreer/.test(PageURL)){

        var links = document.querySelectorAll('a[href*="download.php"]')
        var MP = document.querySelector('.fa.fa-bell.red') ? document.querySelector('.fa.fa-bell.red').closest('a.list-group-item').querySelector('b').textContent : ''

        const getUriWithParam = (baseUrl, params) => {
            //console.log(baseUrl)
            const Url = new URL(baseUrl);
            const urlParams = new URLSearchParams(Url.search);
            for (const key in params) {
                if (params[key] !== undefined) {
                    urlParams.set(key, params[key]);
                }
            }
            Url.search = urlParams.toString();
            return Url.toString();
        };


        for (var i = 0; i < links.length; ++i) {
            links[i].href = await getUriWithParam(links[i].href, {ds: "1"})
            //links[i].href = await getUriWithParam(links[i].href, {js: "on"})
            //let linkText = links[i].textContent.match(/(.*)\s\(\d+\.\dK\).+/)[1].trim()

            //console.log(linkText, /\s/.test(linkText))
            //if(/\s/.test(linkText)){
            links[i].addEventListener("click", async function(event) {
                event.target.href = await getUriWithParam(event.target.href, {ds: "1"})
                //event.target.href = await getUriWithParam(event.target.href, {js: "on"})
                event.preventDefault()
                event.stopPropagation()
                event.stopImmediatePropagation()
                console.log(event.target)
                DownloadFile(event.target.href, event.target)
            })
            //}
        }

        if(!links?.length){
            await sleep(5000)
            window.close()

        }
        else{
            for (var j = 0; j < links.length; ++j) {
                if(!MP){
                    await sleep(5000)
                    DownloadFile(links[j].href, links[j])
                }
                else if( MP > 0){
                    DownloadFile(links[j].href, links[j])
                }
                else{
                    await sleep(5000)
                    window.close()
                }
            }
        }
    }
}

function DownloadFile(url, target) {
    target.querySelector('i.fa').classList.remove('fa-download')
    target.querySelector('i.fa').classList.add('fa-spinner')
    target.querySelector('i.fa').style.setProperty('color', 'White', 'important');

    fetch(url,{
        credentials: 'include',
    })
        .then(async res => {
        var disposition = await res.headers.get('Content-Disposition')
        console.log(disposition)
        target.querySelector('i.fa').style.setProperty('color', 'Orange', 'important');
        if(typeof disposition !== 'undefined' && disposition !== null){
            filename = disposition.split(/;(.+)/)[1].split(/=(.+)/)[1].replace("utf-8''", '').replace(/['"]/g, '').replace(/\+/g, '%20')
            filename = decodeURIComponent(filename)
            //console.log(filename)
            return res.blob()
        }
    })
        .then(async blob => {
        await saveAs(blob, filename)
        target.querySelector('i.fa').classList.remove('fa-spinner')
        target.querySelector('i.fa').classList.add('fa-check-square')
        await sleep(10000)
        window.close()
    })
        .catch((error) => {
        console.error('Downloading Error', error);
    });
}


function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

function getOffset(el) {
    const rect = el.getBoundingClientRect();
    return {
        left: rect.left + window.scrollX,
        top: rect.top + window.scrollY,
        right: rect.right + window.scrollX,
        width: rect.width,
        height: rect.height
    }
}

async function OpenLinks(LinkDB){
    document.querySelector('.OpenIcon').style.setProperty("color", "Orange", "important")
    for (var i = 0; i < LinkDB.length; i++) {
        LinkUrl = LinkDB[i].href
        console.log(LinkUrl)
        await GM_openInTab( LinkUrl , { active: false, insert: true } )
        await sleep(100)
    }
}

addJS_Node (null, null, overrideSelectNativeJS_Functions)

function overrideSelectNativeJS_Functions () {
    window.confirm = function alert (message) { return true; }
}

function addJS_Node (text, s_URL, funcToRun) {
    var D = document;
    var scriptNode = D.createElement ('script');
    scriptNode.type = "text/javascript";
    if (text) scriptNode.textContent = text;
    if (s_URL) scriptNode.src = s_URL;
    if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}