Video Quality Fixer for Twitter

Force highest quality playback for Twitter videos.

目前為 2021-06-03 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name                Video Quality Fixer for Twitter
// @name:zh             Twitter 视频画质修复
// @name:zh-CN          Twitter 视频画质修复
// @namespace           https://github.com/flyhaozi
// @version             0.1.1
// @description         Force highest quality playback for Twitter videos.
// @description:zh      强制 Twitter 播放最高画质的视频
// @description:zh-CN   强制 Twitter 播放最高画质的视频
// @author              flyhaozi
// @match               https://twitter.com/*
// @grant               unsafeWindow
// ==/UserScript==

(function() {
    var realOpen = unsafeWindow.XMLHttpRequest.prototype.open;
    unsafeWindow.XMLHttpRequest.prototype.open = function() {
        var url = arguments['1'];
        url.startsWith("https://video.twimg.com") 
        && url.includes("m3u8?tag=") 
        && this.addEventListener('readystatechange', function(e) {
            if ( this.readyState === 4 ) {
                var originalText = e.target.responseText;
                var lines = originalText.split(new RegExp('\\r?\\n'));
                var modifiedText = lines[0] + '\r\n' 
                                + lines[1] + '\r\n' 
                                + lines[lines.length - 3] + '\r\n' 
                                + lines[lines.length - 2] + '\r\n';

                console.log("■ Video Quality Fixer for Twitter ■");
                console.log("playlist: "+ url + "\n" + "best quality: " + lines[lines.length - 3]);

                Object.defineProperty(this, 'response',     {writable: true});
                Object.defineProperty(this, 'responseText', {writable: true});
                this.response = this.responseText = modifiedText;
            }
        });
        return realOpen.apply(this, arguments);
    };

    // add a mark helps identify if userscript loaded successfully
    var sign = document.createElement("div");
    sign.innerText = "HD";
    sign.style = "position: fixed; right: 0; bottom: 0; color: grey";
    document.querySelector('body').appendChild(sign);
})();