Greasy Fork

Greasy Fork is available in English.

動畫瘋-自動觀看廣告

自動觀看動畫瘋廣告

当前为 2020-08-13 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         動畫瘋-自動觀看廣告
// @namespace    https://shinoharahare.github.io/
// @version      0.2
// @description  自動觀看動畫瘋廣告
// @author       Hare
// @match        https://ani.gamer.com.tw/animeVideo.php?sn=*
// @match        https://imasdk.googleapis.com/js/core/bridge*
// @grant        none
// ==/UserScript==

(async () => {
    'use strict';

    if (location.href.includes('ani.gamer.com.tw')) {
        if (await checkAD()) {
            registerMessageHandlerTop()
            await trigger('#adult', 'click')
            document.querySelector('video[title=Advertisement]').addEventListener('play', ({ target }) => target.muted = true)

            await sleep(30000)

            await postMessage2Ima({ type: 'skip' })

            const video = document.querySelector('#ani_video_html5_api')
            video.addEventListener('loadeddata', () => {
                const t = () => video.pause() || video.removeEventListener('play', t)
                video.addEventListener('play', t)
            })
        }

    } else if (location.href.includes('imasdk.googleapis.com')) {
        if (window.self != window.top) {
            registerMessageHandlerIma()
        }
    }
})();

function registerMessageHandlerTop() {
    window.resolves = {}
    window.addEventListener('message', ({ data }) => {
        const id = data._id
        if (id in window.resolves) {
            window.resolves[id]()
            delete window.resolves[id]
        }
    })
}

function registerMessageHandlerIma() {
    window.addEventListener('message', ({ data, source }) => {
        if (data._id) {
            switch (data.type) {
                case 'skip': {
                    document.querySelector('.videoAdUiSkipButton').click()
                    break
                }
            }
            source.postMessage(data, 'https://ani.gamer.com.tw')
        }
    })
}

function postMessage2Ima(msg) {
    const win = document.querySelector('iframe[src^="https://imasdk.googleapis.com/js/core/"]').contentWindow
    return new Promise(resolve => {
        const id = performance.now()
        window.resolves[id] = resolve
        win.postMessage({
            _id: id,
            ...msg
        }, 'https://imasdk.googleapis.com')
    })
}

async function checkAD() {
    const res = await fetch(`/ajax/token.php?sn=${animefun.videoSn}`)
    const json = await res.json()
    return json.time == 0
}

function trigger(selector, action) {
    return new Promise(resolve => {
        let i = setInterval(() => {
            let el = document.querySelector(selector)
            if (el) {
                el[action]()
                clearInterval(i)
                resolve()
            }
        }, 500)
    })
}

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