diff --git a/src/js/player.js b/src/js/player.js index 07a9e03a1f..49999994bc 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -612,6 +612,30 @@ class Player extends Component { this.tech_ = false; } + /** + * Return a reference to the current tech. + * It will only return a reference to the tech if given an object with the + * `IWillNotUseThisInPlugins` property on it. This is try and prevent misuse + * of techs by plugins. + * + * @param {Object} + * @return {Object} The Tech + * @method tech + */ + tech(safety) { + if (safety && safety.IWillNotUseThisInPlugins) { + return this.tech_; + } + let errorText = ` + Please make sure that you are not using this inside of a plugin. + To disable this alert and error, please pass in an object with + \`IWillNotUseThisInPlugins\` to the \`tech\` method. See + https://github.com/videojs/video.js/issues/2617 for more info. + `; + window.alert(errorText); + throw new Error(errorText); + } + /** * Set up click and touch listeners for the playback element * diff --git a/test/unit/player.test.js b/test/unit/player.test.js index 76b6007479..b4b528fffb 100644 --- a/test/unit/player.test.js +++ b/test/unit/player.test.js @@ -7,6 +7,7 @@ import MediaError from '../../src/js/media-error.js'; import Html5 from '../../src/js/tech/html5.js'; import TestHelpers from './test-helpers.js'; import document from 'global/document'; +import window from 'global/window'; import Tech from '../../src/js/tech/tech.js'; import TechFaker from './tech/tech-faker.js'; @@ -861,3 +862,25 @@ test('you can clear error in the error event', function() { log.error.restore(); }); + +test('Player#tech will return tech given the appropriate input', function() { + let tech_ = {}; + let returnedTech = Player.prototype.tech.call({tech_}, {IWillNotUseThisInPlugins: true}); + + equal(returnedTech, tech_, 'We got back the tech we wanted'); +}); + +test('Player#tech alerts and throws without the appropriate input', function() { + let alertCalled; + let oldAlert = window.alert; + window.alert = () => alertCalled = true; + + let tech_ = {}; + throws(function() { + Player.prototype.tech.call({tech_}); + }, new RegExp('https://github.com/videojs/video.js/issues/2617'), + 'we threw an error'); + + ok(alertCalled, 'we called an alert'); + window.alert = oldAlert; +});