아브자막 다운로더

아브자막 다운로더 도우미

当前为 2022-06-13 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         아브자막 다운로더
// @namespace    아브자막 다운로더
// @version      1.4
// @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
// @grant        GM_setValue
// @grant        GM_getValue
// @license      MIT
// @run-at       document-end
// ==/UserScript==

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

GM_addStyle (`

.IconSet {
    cursor: pointer;
    font-size: 1.5rem;
}

.OnOff, .OpenIcon, .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;

var filename, i, LinkUrl
var OnOff = GM_getValue("OnOff", 'On')

window.onload = function() {
    Start()
}



async function Start(){

    if(!/wr_id/.test(PageURL) && /jamakbos|jamakuser|bigsub|jamak_sujung|jamakfreer/.test(PageURL)){
        document.body.insertAdjacentHTML('beforeend', '<div class="IconSet"></>')
        document.querySelector('.IconSet').insertAdjacentHTML('beforeend', '<i class="OnOff fas fa-power-off"></>')
        document.querySelector('.OnOff').insertAdjacentHTML('beforeend', '&nbsp;&nbsp;<i class="OpenIcon fas fa-external-link-alt"></>')
        document.querySelector('.OpenIcon').insertAdjacentHTML('beforeend', '&nbsp;&nbsp;<i class="NextPage fas fa-arrow-circle-right"></>')
        //document.querySelector('.OpenIcon').style.setProperty("top", '10px' )
        let TitlePostion = getOffset(document.querySelector('.panel-heading a'))
        let IconPostion = getOffset(document.querySelector(".IconSet"))
        document.querySelector('.IconSet').style.setProperty('color', 'dodgerblue', 'important');
        $('.IconSet').css({
            "top": TitlePostion.top
            ,"left": TitlePostion.left + TitlePostion.width - 75
            , "position": "absolute"

        })
        if(OnOff == 'On'){
            document.querySelector('.OnOff').style.setProperty('color', 'dodgerblue', 'important');
            document.querySelector('.OpenIcon').style.visibility = "visible"
        }
        else{
            document.querySelector('.OnOff').style.setProperty('color', 'Gray', 'important');
            document.querySelector('.OpenIcon').style.visibility = "hidden"
        }

        document.querySelector('.OnOff').addEventListener("click", function(event) {
            event.preventDefault()
            event.stopPropagation()
            event.stopImmediatePropagation()
            if(OnOff == 'On'){
                document.querySelector('.OnOff').style.setProperty('color', 'Gray', 'important');
                document.querySelector('.OpenIcon').style.visibility = "hidden"
                OnOff = GM_setValue("OnOff", 'Off')
                OnOff = GM_getValue("OnOff")
            }
            else{
                document.querySelector('.OnOff').style.setProperty('color', 'dodgerblue', 'important');
                document.querySelector('.OpenIcon').style.visibility = "visible"
                OnOff = GM_setValue("OnOff", 'On')
                OnOff = GM_getValue("OnOff")
            }
        })

        document.querySelector('.OpenIcon').addEventListener("click", function(event) {
            event.preventDefault()
            event.stopPropagation()
            event.stopImmediatePropagation()
            OpenLinks(LinkDB)
        })

        document.querySelector('i.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
        })


        window.addEventListener("resize", function(e) {
            let TitlePostion = getOffset(document.querySelector('.panel-heading a'))
            let IconPostion = getOffset(document.querySelector(".IconSet"))
            $('.IconSet').css({
                "top": TitlePostion.top
                ,"left": TitlePostion.left + TitlePostion.width - 75
                , "position": "absolute"

            })
        })

        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) && OnOff == 'On'){

        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);
}