Created
February 4, 2018 01:31
-
-
Save lovemyliwu/34f1d556b411b1fafbe544ea59a1dc65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Baidu YunPan Video Player + | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description auto play next video | |
// @author Smite | |
// @match https://pan.baidu.com/play/video* | |
// @grant none | |
// ==/UserScript== | |
// === utils === | |
function getPlayerState() { | |
var player = window.videojs.getPlayers(); | |
if (player && player.html5player) { | |
let that = player.html5player.el_.querySelector('#html5player_html5_api'); | |
return (that.currentTime > 0 && that.paused && that.ended && that.readyState > 2) ? 'ready' : 'playing'; | |
} | |
return undefined; | |
} | |
function getFillZeroOnLeftString(number, length) { | |
for (;length > number.length;) | |
number = '0' + number; | |
return number; | |
} | |
function getFillZeroList(list) { | |
for (var idx = 0; idx < list.length; idx ++) { | |
list[idx] = getFillZeroOnLeftString(list[idx], 3); | |
} | |
return list; | |
} | |
function getMatchNumberList(string) { | |
return string.match(/\d+/g); | |
} | |
// === sort video items === | |
function sortVideoItems() { | |
var items = $('#videoListView .video-item').detach(); | |
$('#videoListView').append(items.sort(function (itemA, itemB) { | |
var itemANumberList = getMatchNumberList($(itemA).attr('title')), | |
itemBNumberList = getMatchNumberList($(itemB).attr('title')); | |
return getFillZeroList(itemANumberList) >= getFillZeroList(itemBNumberList) ? 1 : -1; | |
})); | |
} | |
(function checkVideoList() { | |
if ('none' === $('#videoListView-tips').css('display') && $('#videoListView .load').length) { | |
sortVideoItems(); | |
} else { | |
setTimeout(checkVideoList, 2E3); | |
} | |
})(); | |
// === when player end start next === | |
function playNextItem() { | |
$('#videoListView .currentplay').next().find('.video-list-thumbnail').click(); | |
} | |
$(document).on('player-state-changed', function (event, states) { | |
if ((states.before === 'playing' || states.before === 'buffering') && states.current === 'ready' && $('#autoplay').prop('checked')) { | |
playNextItem(); | |
} | |
}); | |
(()=>{ | |
function watchPlayerState(before) { | |
var state = getPlayerState(); | |
console.log('>> ', state); | |
if (before && state && before != state) { | |
$(document).trigger('player-state-changed', {before: before, current: state}); | |
} | |
// after exit full screen sometimes can't get state | |
state = state || before; | |
setTimeout(function () { | |
watchPlayerState(state); | |
}, 2E3); | |
} | |
function wrapper() { | |
try{ | |
watchPlayerState(); | |
}catch(error){ | |
setTimeout(wrapper, 2E3); | |
} | |
} | |
wrapper(); | |
})(); | |
// === interactive === | |
$('.video-toolbar-buttonbox').append('<input type="checkbox" id="autoplay" /><span style="color: red;">自动播放下一集</span>'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment