Wadsworth Constant

Script adds a posibility to apply Wadsworth Constant to YouTube videos. Two buttons are added, one to apply and to auto-switch.

当前为 2015-03-23 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Wadsworth Constant
// @description     Script adds a posibility to apply Wadsworth Constant to YouTube videos. Two buttons are added, one to apply and to auto-switch.
// @namespace   Wadsworth Constant
// @include http*://*.youtube.com/*
// @include http*://www.youtube.com/*
// @include http*://youtube.com/*
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_xmlhttpRequest
// @noframes
// @run-at document-end
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
// @author      Dexmaster
// @co-author   Jake Lauer
// @version     1.1.4
// ==/UserScript==
(function () {
    'use strict';
    var c_loc = window.location.search, loaded = false;

    function ww_apply(apply) {
        var url; //,saveURL
        if (!!apply) {
            url = window.location.href;
            if (url.match('#') || url.match('&t')) {
                url = url.split('#') [0].split('&t') [0];
            }
            url = url.split('?') [1].split('&') [0].split('=') [1];
            url = 'https://gdata.youtube.com/feeds/api/videos/' + url;
            //console.log(url);
            if (url === 'https://gdata.youtube.com/feeds/api/videos/') {
                //console.log('Something went wrong... dunno what it was. Sorry!');
            }
            window.location.hash = '#';
            //console.log(saveURL);
            GM_xmlhttpRequest({
                method: 'GET',
                // ignoreCache: true,
                redirectionLimit: 0,
                url: url,
                onerror: function (res) {
                    var msg = 'An error occurred.' + '\nresponseText: ' + res.responseText + '\nreadyState: ' + res.readyState + '\nresponseHeaders: ' + res.responseHeaders + '\nstatus: ' + res.status + '\nstatusText: ' + res.statusText + '\nfinalUrl: ' + res.finalUrl;
                    //console.log(msg);
                },
                onload: function (xhr) {

                    if (xhr.readyState === 4) {
                        if (xhr.status === 200 && xhr.responseText && xhr.responseText.length > 20) {
                            var results = xhr.responseText,
                                duration = parseInt(results.split('duration seconds=\'') [1].split('\'') [0]),
                                newTime = Math.ceil(duration * 0.3),
                                hrr = window.location.href,
                                mrr = '';
                            //console.log(xhr);
                            if (hrr.match('&t=')) {
                                hrr = hrr.split('&t=') [0];
                                if (hrr.split('&t=').length > 1) {
                                    mrr = hrr.split('&t=') [1];
                                    hrr += (mrr.match('&')) ? '&' + mrr.split('&') [1] : '';
                                }
                            }
                            if (hrr.match('#')) {
                                window.location = (hrr.split('#') [0] + '#t=' + newTime + 's');
                            } else {
                                window.location = (hrr + '#t=' + newTime + 's');
                            }
                        }
                    }
                }
            });
        }
    }

    function pp_state(event) {
        var t_loc = window.location, yt_wadsworth = GM_getValue('yt_wadsworth', false);
        if (c_loc !== t_loc.search || !loaded) {
            if(!loaded)loaded = true;
            if (yt_wadsworth) {
                ww_apply(true);
                // history changed because of pushState/replaceState
            }
        }
    }

    function ww_switch() {
        var yt_wadsworth = GM_getValue('yt_wadsworth', false);
        GM_setValue('yt_wadsworth', !yt_wadsworth);
        jQuery('#switch_wadsworth').text('Switch ' + (!yt_wadsworth ? 'On' : 'Off'));
        ww_apply(!yt_wadsworth);
    }

    function load_once() {
        if(loaded) return;
        var yt_wadsworth = GM_getValue('yt_wadsworth', false);
        jQuery('#content').before('<div id="block_wadsworth" style="position: relative; z-index: 1; height: 0px;" class="content-alignment watch-small">Wadsworth Constant: <div id="apply_wadsworth" style="display: inline-block; border: 1px solid red; cursor: pointer; background: none repeat scroll 0% 0% rgb(204, 204, 204); color: black; z-index: 12; padding: 4px 6px; position: relative; margin-top: 3px;">Apply</div><div id="switch_wadsworth" style="display: inline-block; border: 1px solid red; cursor: pointer; background: none repeat scroll 0% 0% rgb(204, 204, 204); color: black; z-index: 12; padding: 4px 6px; position: relative; margin-left: 6px; margin-top: 3px;">Switch ' + (yt_wadsworth ? 'On' : 'Off') + '</div></div>');
        if (!window.location.href.match('v=') && !window.location.href.match('#p')) {
            jQuery('#block_wadsworth').remove();
        }
        pp_state();
        jQuery('#switch_wadsworth').live('click', ww_switch);
        jQuery('#apply_wadsworth').live('click', function () {
            ww_apply(true);
        });
    }

    document.addEventListener('DOMContentLoaded', load_once);
    window.addEventListener('spfdone', pp_state);
    //window.addEventListener('popstate', pp_state);
}) ();