Button change speed video

Button change speed videos

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);