Greasy Fork

Greasy Fork is available in English.

動畫瘋工具箱

RT

当前为 2018-03-11 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         動畫瘋工具箱
// @namespace    https://blog.maple3142.net/
// @version      0.4
// @description  RT
// @author       maple3142
// @match        https://ani.gamer.com.tw/animeVideo.php?sn=*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/m3u8-parser.min.js
// @grant        none
// ==/UserScript==

; (function ($) {
	'use strict'
	//in order to get videojs instance
	requirejs.config({
		baseUrl: '//i2.bahamut.com.tw',
		waitSeconds: 0,
		paths: {
			order: 'js/order'
		},
		shim: {
			viblast: {},
			vastvpaid: {
				deps: ['videojs']
			}
		}
	})
	requirejs(['order!videojs'], videojs => hookSetter(videojs.players, 'ani_video', function onAniVideo(vid) {
		window.ani_video = vid //EXPOSE
		hookSetter(vid.K, 'src', onPlaylistUrl)
	}))
	function hookSetter(obj, prop, cb) {
		Object.defineProperty(obj, prop, {
			set: cb
		})
	}
	function render(pls) {
		const html = pls
			.map(
				pl =>
					`<div><label for="${pl.res.height}p">${pl.res.height}P: </label><input id="${
					pl.res.height
					}p" value="${pl.url}" style="width: 500px;"></div>`
			)
			.join('')
		$('.anime_name').append(`<div id="anigamer_m3u8_warpper">${html}</div>`)
	}
	function cvtM3U8_to_playlist(baseurl) {
		return m3u8 => {
			const parser = new m3u8Parser.Parser()
			parser.push(m3u8)
			parser.end()
			const pls = parser.manifest.playlists.map(pl => ({
				url: 'https:' + baseurl + pl.uri,
				res: pl.attributes.RESOLUTION
			}))
			return pls
		}
	}
	function onPlaylistUrl(playlisturl) {
		if (playlisturl.indexOf('gamer_ad') !== -1) {
			//is ad
			return
		}
		const baseurl = playlisturl.replace(/index\.m3u8.*/, '')
		fetch(playlisturl)
			.then(r => r.text())
			.then(cvtM3U8_to_playlist(baseurl))
			.then(pls => window.M3U8_PLAYLIST = pls) //EXPOSE
			.then(render)
	}

	function triggerDownload(url, fname) {
		const a = document.createElement('a')
		a.href = url
		a.download = fname
		document.body.appendChild(a)
		a.click()
		a.remove()
	}
	function saveTextAsFile(text, fname) {
		const blob = new Blob([text])
		const url = URL.createObjectURL(blob)
		triggerDownload(url, fname)
		URL.revokeObjectURL(url)
	}
	//extra: add a button to download danmu as json file
	hookSetter(animefun, 'danmu', danmu => {
		const text = JSON.stringify(danmu)
		const title = $('.anime_name h1').text()
		$('.anime_name').append($('<a href="javascript:void(0)">把彈幕存成檔案</a>').on('click', () => {
			saveTextAsFile(text, `${title}_彈幕.json`)
		}))
	})



	//extra: block anti adblock alert
	const orig_alert = alert
	alert = function (t) {
		if (t.includes('由於擋廣告插件會影響播放器運作')) return
		orig_alert(t)
	}
})(jQuery)