<commit_before>﻿//  This content script runs against YouTube's video iframe before any of their scripts have loaded.
//  It forces YouTube to use the HTML5 video player, hooks into requests sent from their server,
//  and attempts to listen for interesting events on the <video> itself. If the <video> element
//  is not found then it will poll for its existence for a few seconds before emitting an error.
(function() {
    'use strict';

    var YouTubeIFrameContentScript = function() {
        //  Record any errors emitted by YouTube
        this.errors = [];
        //  The port used for external communication w/ the extension.
        this.port = null;
        //  The <video> hosting the active YouTube video
        this.videoStream = null;

        //  This is a heavy-handed approach for ensuring that Streamus will *always* be given an HTML5 player by YouTube if at all possible.
        //  By overloading canPlayType and forcing it to return 'probably' - YouTube will always assume that the browser can handle HTML5 video.
        //  Since Streamus only runs in browsers which support HTML5 video - canPlayType technically should always return 'probably'.
        //  However, sometimes other extensions or custom browsers (i.e. Slimjet) mess with the canPlayType prototype and prevent YouTube from loading HTML5.
        //  If Flash loads then Streamus is doomed. So, it's OK to be heavy-handed and potentially break stuff because the options are either "make it work" or "already broken"
        this.patchVideoCanPlayType = function() {
            var script = document.createElement('script');
            script.innerHTML = 'HTMLMediaElement.prototype.canPlayType = function() { return "probably"; };';
            document.head.appendChild(script);
        }.bind(this);

        //  Append a script to the page which will intercept YouTube's server requests and post messages out of the iframe with details about those requests.
        //  Needs to be an appended script because content scripts are sandboxed in such a way that they do not share variables with appended scripts.
        this.appendInterceptorScript = function() {
            //var interceptorScript = document.createElement('script');
            //interceptorScript.src = chrome.runtime.getURL('js/contentScript/interceptor.js');
            //document.head.appendChild(interceptorScript);
        }.bind(this);

        //  Attach event listeners to the <video> element.
        this.monitorVideoStream = function() {
            var lastPostedCurrentTime = null;

            this.videoStream.addEventListener('loadstart', function() {
                lastPostedCurrentTime = null;
            }.bind(this));

            this.videoStream.addEventListener('timeupdate', function() {
                //  Round currentTime to the nearest second to prevent flooding the port with unnecessary messages.
                var currentTime = Math.ceil(this.videoStream.currentTime);

                if (currentTime !== lastPostedCurrentTime) {
                    this.port.postMessage({
                        currentTime: currentTime
                    });

                    lastPostedCurrentTime = currentTime;
                }
            }.bind(this));

            this.videoStream.addEventListener('seeking', function() {
                this.port.postMessage({
                    seeking: true
                });
            }.bind(this));

            this.videoStream.addEventListener('seeked', function() {
                this.port.postMessage({
                    seeking: false
                });
            }.bind(this));
        }.bind(this);

        //  Create the port needed to communicate with the parent extension.
        this.initializePort = function() {
            this.port = chrome.runtime.connect({
                name: 'youTubeIFrameConnectRequest'
            });

            //  The extension can request the *exact* time of YouTube's video player.
            //  Respond with that value, but also include a timestamp to account for the time it takes to send the postMessage.
            this.port.onMessage.addListener(function(message) {
                if (message === 'getCurrentTimeHighPrecision') {
                    var currentTime = this.videoStream === null ? 0 : this.videoStream.currentTime;

                    this.port.postMessage({
                        timestamp: Date.now(),
                        currentTimeHighPrecision: currentTime
                    });
                }
            }.bind(this));
        }.bind(this);

        //  If YouTube fails to initialize properly - notify the extension so that logs can be taken.
        //  Include any errors encountered to help with debugging.
        this.notifyYouTubeLoadFailure = function() {
            this.port.postMessage({
                error: 'videoStream not found. Errors: ' + this.errors.join(', ')
            });
        }.bind(this);<commit_msg>Errors not being output because complex type<commit_after>﻿//  This content script runs against YouTube's video iframe before any of their scripts have loaded.
//  It forces YouTube to use the HTML5 video player, hooks into requests sent from their server,
//  and attempts to listen for interesting events on the <video> itself. If the <video> element
//  is not found then it will poll for its existence for a few seconds before emitting an error.
(function() {
    'use strict';

    var YouTubeIFrameContentScript = function() {
        //  Record any errors emitted by YouTube
        this.errors = [];
        //  The port used for external communication w/ the extension.
        this.port = null;
        //  The <video> hosting the active YouTube video
        this.videoStream = null;

        //  This is a heavy-handed approach for ensuring that Streamus will *always* be given an HTML5 player by YouTube if at all possible.
        //  By overloading canPlayType and forcing it to return 'probably' - YouTube will always assume that the browser can handle HTML5 video.
        //  Since Streamus only runs in browsers which support HTML5 video - canPlayType technically should always return 'probably'.
        //  However, sometimes other extensions or custom browsers (i.e. Slimjet) mess with the canPlayType prototype and prevent YouTube from loading HTML5.
        //  If Flash loads then Streamus is doomed. So, it's OK to be heavy-handed and potentially break stuff because the options are either "make it work" or "already broken"
        this.patchVideoCanPlayType = function() {
            var script = document.createElement('script');
            script.innerHTML = 'HTMLMediaElement.prototype.canPlayType = function() { return "probably"; };';
            document.head.appendChild(script);
        }.bind(this);

        //  Append a script to the page which will intercept YouTube's server requests and post messages out of the iframe with details about those requests.
        //  Needs to be an appended script because content scripts are sandboxed in such a way that they do not share variables with appended scripts.
        this.appendInterceptorScript = function() {
            //var interceptorScript = document.createElement('script');
            //interceptorScript.src = chrome.runtime.getURL('js/contentScript/interceptor.js');
            //document.head.appendChild(interceptorScript);
        }.bind(this);

        //  Attach event listeners to the <video> element.
        this.monitorVideoStream = function() {
            var lastPostedCurrentTime = null;

            this.videoStream.addEventListener('loadstart', function() {
                lastPostedCurrentTime = null;
            }.bind(this));

            this.videoStream.addEventListener('timeupdate', function() {
                //  Round currentTime to the nearest second to prevent flooding the port with unnecessary messages.
                var currentTime = Math.ceil(this.videoStream.currentTime);

                if (currentTime !== lastPostedCurrentTime) {
                    this.port.postMessage({
                        currentTime: currentTime
                    });

                    lastPostedCurrentTime = currentTime;
                }
            }.bind(this));

            this.videoStream.addEventListener('seeking', function() {
                this.port.postMessage({
                    seeking: true
                });
            }.bind(this));

            this.videoStream.addEventListener('seeked', function() {
                this.port.postMessage({
                    seeking: false
                });
            }.bind(this));
        }.bind(this);

        //  Create the port needed to communicate with the parent extension.
        this.initializePort = function() {
            this.port = chrome.runtime.connect({
                name: 'youTubeIFrameConnectRequest'
            });

            //  The extension can request the *exact* time of YouTube's video player.
            //  Respond with that value, but also include a timestamp to account for the time it takes to send the postMessage.
            this.port.onMessage.addListener(function(message) {
                if (message === 'getCurrentTimeHighPrecision') {
                    var currentTime = this.videoStream === null ? 0 : this.videoStream.currentTime;

                    this.port.postMessage({
                        timestamp: Date.now(),
                        currentTimeHighPrecision: currentTime
                    });
                }
            }.bind(this));
        }.bind(this);

        //  If YouTube fails to initialize properly - notify the extension so that logs can be taken.
        //  Include any errors encountered to help with debugging.
        this.notifyYouTubeLoadFailure = function() {
            this.port.postMessage({
                error: 'videoStream not found. Errors: ' + JSON.stringify(this.errors)
            });
        }.bind(this);
{'fraction_non_alphanumeric': 0.08258258258258258, 'fraction_numerical': 0.0012012012012012011, 'mean_word_length': 2.0798397040690504, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3853505', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2202, 'n_words': 861}