Button change speed video

Button change speed videos

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Button change speed video
// @namespace    change_speed
// @version      1
// @description  Button change speed videos
// @author       nht
// @include      *
// @grant        unsafeWindow
// @grant        GM_registerMenuCommand
// @run-at       document-start
// ==/UserScript==


GM_registerMenuCommand ("Active Speed", show_ui, "A");

function changeSpeed(self){
    var iframes = unsafeWindow.document.querySelectorAll('iframe');
    var sendData = {id: "my_speed", speed: self.value};
    for(var i = 0; i < iframes.length; i++){
        try{
            iframes[i].contentWindow.postMessage(sendData, "*");
        }catch(e) { console.log(e);}
    }
    changeSpeedIframe(self.value);
}

function changeSpeedIframe(data){

    var speed = parseFloat(data);
    if(isNaN(speed)) {
        return;
    }
    //console.log("find video tag");
    var videos = unsafeWindow.document.querySelectorAll('video');
    //console.log(videos);
    console.log("speed ", speed);
    for(var i = 0; i < videos.length; i++){
        videos[i].playbackRate = speed;
    }
}
unsafeWindow.window.changeSpeed = changeSpeed;
unsafeWindow.window.changeSpeedIframe = changeSpeedIframe;
//event on iframe
if (window.top !== window.self){
    unsafeWindow.window.onmessage = function(event){
        if (event.data && event.data.id === "my_speed"){
            changeSpeedIframe(event.data.speed);
        }
    }
}

function show_ui() {
    var Speeds = ["1.00", "1.25", "1.50", "2.00", "3.00", "500.0"];
    if (window.top === window.self) {
        var div = unsafeWindow.document.createElement("div");
        div.innerHTML = "";
        for(var i = 0; i < Speeds.length; i++){
            div.innerHTML += '<button style="margin-left: 5px; margin-bottom: 5px;" type="button" value="' + Speeds[i] + '" onClick=changeSpeed(this)>x' + Speeds[i] + '</button><br>';
        }
        div.setAttribute("style","z-index:9999; position: -webkit-sticky; position:fixed; bottom: 2px; left:0; font-size: 15px; color: black;");
        //unsafeWindow.document.body.insertBefore(div, unsafeWindow.document.body.firstChild);
        unsafeWindow.document.body.appendChild(div);
    }
}
//unsafeWindow.window.show_ui = show_ui;
//unsafeWindow.document.addEventListener('DOMContentLoaded', show_ui);