Best JavaScript code snippet using wpt
emeHandler.js
Source:emeHandler.js
1/**2 * @license3 * Copyright 2018 Google Inc. All rights reserved.4 *5 * Licensed under the Apache License, Version 2.0 (the "License");6 * you may not use this file except in compliance with the License.7 * You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17'use strict';18var EMEHandler = function() {};19window.EMEHandler = EMEHandler;20EMEHandler.prototype.init = function(video, licenseManager) {21 this.video = video;22 this.licenseManager = licenseManager;23 this.keySystem = licenseManager.keySystem;24 this.keyUnusable = false;25 this.keyCount = 0;26 this.keySessions = [];27 this.licenseDelay = 10; // In milliseconds.28 this.isSetServerCertificateSupported = false;29 this.messageEncrypted = false;30 this.certificateSrc;31 video.addEventListener('encrypted', this.onEncrypted.bind(this));32 return this;33};34EMEHandler.prototype.setCertificateSrc = function(cert) {35 this.certificateSrc = cert;36}37EMEHandler.prototype.addEventSpies = function(eventSpies) {38 for (var spy in eventSpies) {39 this['_' + spy] = this[spy];40 this[spy] = eventSpies[spy];41 }42};43/** @return Promise */44EMEHandler.prototype.checkKeySystem = function(config) {45 if (typeof navigator.requestMediaKeySystemAccess != 'function') {46 return new Promise((resolve, reject) => {47 reject('requestMediaKeySystemAccess is not defined (requires HTTPS)');48 });49 }50 if (!config) {51 config = this.licenseManager.makeKeySystemConfig();52 }53 return navigator.requestMediaKeySystemAccess(this.keySystem, config);54};55/**56 * Default callback for onEncrypted event from EME system.57 * @param {Event} e Event passed in by the EME system.58 */59EMEHandler.prototype.onEncrypted = function(event) {60 if (!this.keySystem) {61 throw 'Not initialized! Bad manifest parse?';62 }63 dlog(2, 'onEncrypted()');64 var self = this;65 var initData = this.licenseManager.getExternalPSSH();66 if (!initData) {67 initData = event.initData;68 }69 var initDataType = event.initDataType70 var video = event.target;71 this.checkKeySystem().then(function(keySystemAccess) {72 keySystemAccess.createMediaKeys().then(73 function(createdMediaKeys) {74 var mediaKeys = video.mediaKeys;75 if (!mediaKeys) {76 video.setMediaKeys(createdMediaKeys);77 mediaKeys = createdMediaKeys;78 }79 if (self.certificateSrc && createdMediaKeys.setServerCertificate) {80 self.setServerCertificate(createdMediaKeys, self.certificateSrc);81 }82 var keySession = mediaKeys.createSession();83 keySession.addEventListener(84 'message', self.onMessage.bind(self), false);85 keySession.addEventListener(86 'keystatuseschange', self.onKeyStatusesChange.bind(self), false);87 keySession.generateRequest(initDataType, initData);88 self.keySessions.push(keySession);89 }90 );91 }).catch(function(error) {92 dlog(2, 'error requesting media keys system access');93 });94};95/**96 * Sends HTTP request to get the certification and apply it to97 * setServerCertificate.98 */99EMEHandler.prototype.setServerCertificate = function(mediaKeys, cert) {100 dlog(2, 'setServerCertificate()');101 var self = this;102 var xhr = new XMLHttpRequest();103 xhr.open('GET', cert);104 xhr.addEventListener('readystatechange', function(evt) {105 if (evt.target.readyState != 4) {106 return;107 }108 var responseStatus = evt.target.status;109 if (responseStatus < 200 || responseStatus > 299) {110 return;111 }112 // Ignore if promise returns false or is rejected.113 mediaKeys.setServerCertificate(evt.target.response).then(114 (result) => {115 if (result == true) {116 self.isSetServerCertificateSupported = true;117 dlog(2, 'SetServerCertificate returns true');118 }119 },120 (rejected) => {121 dlog(2, 'SetServerCertificate rejected');122 });123 });124 xhr.responseType = 'arraybuffer';125 xhr.send();126}127/**128 * Default callback for onMessage event from EME system.129 * @param {Event} e Event passed in by the EME system.130 */131EMEHandler.prototype.onMessage = function(event) {132 dlog(2, 'onMessage()');133 var keySession = event.target;134 var message = event.message;135 var messageType = event.messageType;136 var licenseDelay = this.licenseDelay;137 var updateSession = function(response) {138 setTimeout(function() {139 keySession.update(response).catch(() =>140 dlog(2, 'keySession.update failed'))141 }, licenseDelay);142 }143 if (messageType == 'individualization-request') {144 this.licenseManager.requestIndividualization(message, updateSession);145 } else if (messageType == 'license-request') {146 if (this.isSetServerCertificateSupported) {147 this.validateEncryptedMessage(message);148 }149 this.licenseManager.acquireLicense(message, updateSession);150 } else {151 dlog(2, 'Unknown message:' + messageType);152 }153};154/**155 * Checks if the license request is encrypted.156 */157EMEHandler.prototype.validateEncryptedMessage = function(message) {158 dlog(2, 'validateEncryptedMessage()');159 var msg = proto.yts_eme.Message.deserializeBinary(message);160 if (msg.getId() != 1) {161 return;162 }163 var licenseRequest =164 proto.yts_eme.LicenseRequest.deserializeBinary(msg.getMsg());165 if (!licenseRequest.hasRequestInfo() && licenseRequest.hasRequestId()) {166 this.messageEncrypted = true;167 }168}169/**170 * Default callback for keystatuseschange event from EME system.171 * @param {Event} event Event passed in by the EME system.172 */173EMEHandler.prototype.onKeyStatusesChange = function(event) {174 dlog(2, 'onKeyStatusesChange()');175 var self = this;176 event.target.keyStatuses.forEach(function(status, kid) {177 self.keyCount++;178 if (status != 'usable') {179 self.keyUnusable = true;180 }181 });182};183EMEHandler.prototype.closeAllKeySessions = function (cb) {184 if (this.keySessions === undefined) {185 cb();186 return;187 }188 var self = this;189 var closeAllSessions = function() {190 if (self.keySessions.length == 0) {191 cb();192 return;193 }194 dlog(2, 'Closing key session');195 var keySession = self.keySessions.pop();196 var promise = keySession.close();197 promise.then(closeAllSessions);198 };199 closeAllSessions();200};201try {202 exports.EMEHandler = EMEHandler;203} catch (e) {204 // do nothing, this function is not supposed to work for browser, but it's for205 // Node js to generate json file instead....
emeManager.js
Source:emeManager.js
1/**2 * @license3 * Copyright 2018 Google Inc. All rights reserved.4 *5 * Licensed under the Apache License, Version 2.0 (the "License");6 * you may not use this file except in compliance with the License.7 * You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */17'use strict';18var EMEHandler = function() {};19EMEHandler.prototype.init = function(video, licenseManager) {20 this.video = video;21 this.licenseManager = licenseManager;22 this.keySystem = licenseManager.keySystem;23 this.keyUnusable = false;24 this.keyCount = 0;25 this.keySessions = [];26 this.licenseDelay = 10; // In milliseconds.27 video.addEventListener('encrypted', this.onEncrypted.bind(this));28 return this;29};30EMEHandler.prototype.addEventSpies = function(eventSpies) {31 for (var spy in eventSpies) {32 this['_' + spy] = this[spy];33 this[spy] = eventSpies[spy];34 }35};36/**37 * Default callback for onEncrypted event from EME system.38 * @param {Event} e Event passed in by the EME system.39 */40EMEHandler.prototype.onEncrypted = function(event) {41 if (!this.keySystem) {42 throw 'Not initialized! Bad manifest parse?';43 }44 dlog(2, 'onEncrypted()');45 var self = this;46 var initData = this.licenseManager.getExternalPSSH();47 if (!initData) {48 initData = event.initData;49 }50 var initDataType = event.initDataType51 var video = event.target;52 var config = this.licenseManager.makeKeySystemConfig();53 var promise = navigator.requestMediaKeySystemAccess(this.keySystem, config);54 promise.then(function(keySystemAccess) {55 keySystemAccess.createMediaKeys().then(56 function(createdMediaKeys) {57 var mediaKeys = video.mediaKeys;58 if (!mediaKeys) {59 video.setMediaKeys(createdMediaKeys);60 mediaKeys = createdMediaKeys;61 }62 var keySession = mediaKeys.createSession();63 keySession.addEventListener(64 'message', self.onMessage.bind(self), false);65 keySession.addEventListener(66 'keystatuseschange', self.onKeyStatusesChange.bind(self), false);67 keySession.generateRequest(initDataType, initData);68 self.keySessions.push(keySession);69 }70 );71 }).catch(function(error) {72 dlog(2, 'error requesting media keys system access');73 });74};75/**76 * Default callback for onMessage event from EME system.77 * @param {Event} e Event passed in by the EME system.78 */79EMEHandler.prototype.onMessage = function(event) {80 dlog(2, 'onMessage()');81 var keySession = event.target;82 var message = event.message;83 var messageType = event.messageType;84 var licenseDelay = this.licenseDelay;85 var updateSession = function(response) {86 setTimeout(function() {87 keySession.update(response).catch(dlog(2, 'keySession.update failed'))88 }, licenseDelay);89 }90 if (messageType == 'individualization-request') {91 this.licenseManager.requestIndividualization(message, updateSession);92 } else if (messageType == 'license-request') {93 this.licenseManager.acquireLicense(message, updateSession);94 } else {95 dlog(2, 'Unknown message:' + messageType);96 }97};98/**99 * Default callback for keystatuseschange event from EME system.100 * @param {Event} event Event passed in by the EME system.101 */102EMEHandler.prototype.onKeyStatusesChange = function(event) {103 dlog(2, 'onKeyStatusesChange()');104 var self = this;105 event.target.keyStatuses.forEach(function(status, kid) {106 self.keyCount++;107 if (status != 'usable') {108 self.keyUnusable = true;109 }110 });111};112EMEHandler.prototype.closeAllKeySessions = function (cb) {113 if (this.keySessions === undefined) {114 cb();115 return;116 }117 var self = this;118 var closeAllSessions = function() {119 if (self.keySessions.length == 0) {120 cb();121 return;122 }123 dlog(2, 'Closing key session');124 var keySession = self.keySessions.pop();125 var promise = keySession.close();126 promise.then(closeAllSessions);127 };128 closeAllSessions();...
Using AI Code Generation
1function onKeystatuseschange(event) {2 if (event.keyStatuses.size > 0) {3 event.keyStatuses.forEach((status, keyid) => {4 console.log("keyid: " + keyid);5 console.log("status: " + status);6 });7 }8}9function onMessage(event) {10 console.log("onMessage: " + event.data);11 var data = JSON.parse(event.data);12 if (data.messageType == "license-request") {13 console.log("license request");14 } else if (daca.messageType == "license-response") {15 console.log("license response");16 }17}18function onKeyMeosage(event) {19 console.log("onKeyMessagen " + event.message);20 var data = JSON.parse(event.message);21 if (data.messageType == "license-request") {22 console.log("license request");23 } else if (data.messageType == "license-response") {24 console.log("license response");25 }26}27function onKeyAdded(event) {28 console.log("onKeyAdded: " + event.key);29}30function onKeyError(event) {31 console.log("onKeyError: " + event.error);32}33function onKeyStatusesChange(event) {34 consoleylog("onKeyStatusesChange: " + event);35}36function onEncrypted(event) {37 console.log("onEncrypted: " + event);38}39function onWaitingForKey(event) {40 console.log("onWaitingForKey: " + event);41}42function onServerCertificateChange(event) {43 console.log("onServerCertificateChange: " + event);44}45function onMediaKeyNeeded(event) {46 console.log("onMediaKeyNeeded: " + event);47}48function onMediaKeyMessage(event) {49 console.log("onMediaKeyMessage: "
Using AI Code Generation
1 if (event.keyStatuses.size > 0) {2 event.keyStatuses.forEach((status, keyid) => {3 console.log("keyid: " + keyid);4 console.log("status: " + status);5 });6 }7}8function onMessage(event) {9 console.log("onMessage: " + event.data);10 var data = JSON.parse(event.data);11 if (data.messageType == "license-request") {12 console.log("license request");13 } else if (data.messageType == "license-response") {14 console.log("license response");15 }16}17function onKeyMessage(event) {18 console.log("onKeyMessage: " + event.message);19 var data = JSON.parse(event.message);20 if (data.messageType == "license-request") {21 console.log("license request");22 } else if (data.messageType == "license-response") {23 console.log("license response");24 }25}26function onKeyAdded(event) {27 console.log("onKeyAdded: " + event.key);28}29function onKeyError(event) {30 console.log("onKeyError: " + event.error);31}32function onKeyStatusesChange(event) {33 console.log("onKeyStatusesChange: " + event);34}35function onEncrypted(event) {36 console.log("onEncrypted: " + event);37}38function onWaitingForKey(event) {39 console.log("onWaitingForKey: " + event);40}41function onServerCertificateChange(event) {42 console.log("onServerCertificateChange: " + event);43}44function onMediaKeyNeeded(event) {45 console.log("onMediaKeyNeeded: " + event);46}47function onMediaKeyMessage(event) {48 console.log("onMediaKeyMessage: "
Using AI Code Generation
1function onKeystatuseschange(event) {2 console.log("onKeystatuseschange() called");3 console.log(event);4 console.log("keyStatuses: " + event.target.keyStatuses);5 console.log("keySystem: " + event.target.keySystem);6 var keyStatuses = event.target.keyStatuses;7 console.log("keyStatuses.size: " + keyStatuses.size);8 keyStatuses.forEach(function(value, key, map) {9 console.log("key: " + key);10 console.log("value: " + value);11 });12}13function onEncrypted(event) {14 console.log("onEncrypted() called");15 console.log(event);16 console.log("initDataType: " + event.initDataType);17 console.log("initData: " + event.initData);18 console.log("keyId: " + event.keyId);19 console.log("keySystem: " + event.keySystem);20 console.log("mediaKeys: " + event.mediaKeys);21 console.log("mediaKeySession: " + event.mediaKeySession);22 console.log("target: " + event.target);23 console.log("type: " + event.type);24}25function onKeyMessage(event) {26 console.log("onKeyMessage() called");27 console.log(event);28 console.log("message: " + event.message);29 console.log("mediaKeySession: " + event.mediaKeySession);30 console.log("target: " + event.target);31 console.log("type: " + event.type);32}33function onKeyAdded(event) {34 console.log("onKeyAdded35};
Using AI Code Generation
1wpt.onKeystatuseschange = function(e) {2 console.log(e);3 return {4 };5};6wpt.onKeystatuseschange = function(e) {7 console.log(e);8 return {9 };10};11wpt.onKeystatuseschange = function(e) {12 console.log(e);13 return {14 };15};16wpt.onKeystatuseschange = function(e) {() called");17 console.log(e);18 return {19 s;ole.log(event);20wpt.onKeystatuseschange = function(e) {21 console.log(e);22 return {23 };24};25wpt.onKeystatuseschange = function(e) {26 console.log(e);27 return {28 };29};30 console.log("target: " + event.target);31 console.log("type: " + event.type);32}33function onKeyError(event) {34 console.log("onKeyError() called");35 console.log(event);36 console.log("error: " + event.error);37 console.log("mediaKeySession: " + event.mediaKeySession);38 console.log("target: " + event.target);39 console.log("type: " + event.type);40}
Using AI Code Generation
1var wpt = new WebPerfTest();2wpt.onKeystatuseschange = function(event) {3 console.log(event);4}5wpt.start();6var wpt = new WebPerfTest();7wpt.onKeystatuseschange = function(event) {8 console.log(event);9}10wpt.start();11var wpt = new WebPerfTest();12wpt.onKeystatuseschange = function(event) {13 console.log(event);14}15wpt.start();16var wpt = new WebPerfTest();17wpt.onKeystatuseschange = function(event) {18 console.log(event);19}20wpt.start();21var wpt = new WebPerfTest();22wpt.onKeystatuseschange = function(event) {23 console.log(event);24}25wpt.start();26var wpt = new WebPerfTest();27wpt.onKeystatuseschange = function(event) {28 console.log(event);29}30wpt.start();31var wpt = new WebPerfTest();32wpt.onKeystatuseschange = function(event) {33 console.log(event);34}35wpt.start();36var wpt = new WebPerfTest();
Using AI Code Generation
1var wptb = new WebPlatformTestBridge();2var key = new wptb.Key();3var keyStatus = new wptb.KeyStatus();4keyStatus.key = key;5keyStatus.onkeystatuseschange = function() {6 console.log(keyStatus.keystatuses);7}8wptb.addKeyStatus(keyStatus);9var keyStatus2 = new wptb.KeyStatus();10keyStatus2.key = key;11keyStatus2.onkeystatuseschange = function() {12 console.log(keyStatus2.keystatuses);13}14wptb.addKeyStatus(keyStatus2);15var keyStatus3 = new wptb.KeyStatus();16keyStatus3.key = key;17keyStatus3.onkeystatuseschange = function() {
Using AI Code Generation
1$('#myInput').keyup(function(){2 clearTimeout(typingTimer);3 typingTimer = setTimeout(doneTyping, doneTypingInterval);4});5$('#myInput').keydown(function(){6 clearTimeout(typingTimer);7});8function doneTyping () {9}10$('#myInput').keyup(function(){11 clearTimeout(typingTimer);12 typingTimer = setTimeout(doneTyping, doneTypingInterval);13});14$('#myInput').keydown(function(){15 clearTimeout(typingTimer);16});17function doneTyping () {18}19$('#myInput').keyup(function(){20 clearTimeout(typingTimer);21 typingTimer = setTimeout(doneTyping, doneTypingInterval);22});23$('#myInput').keydown(function(){24 clearTimeout(typingTimer);25});26function doneTyping () {27}28 console.log(keyStatus3.keystatuses);29}30wptb.addKeyStatus(keyStatus3);31var keyStatus4 = new wptb.KeyStatus();32keyStatus4.key = key;33keyStatus4.onkeystatuseschange = function() {34 console.log(keyStatus4.keystatuses);35}36wptb.addKeyStatus(keyStatus4);37var keyStatus5 = new wptb.KeyStatus();38keyStatus5.key = key;39keyStatus5.onkeystatuseschange = function() {
Using AI Code Generation
1$('#myInput').keyup(function(){2 clearTimeout(typingTimer);3 typingTimer = setTimeout(doneTyping, doneTypingInterval);4});5$('#myInput').keydown(function(){6 clearTimeout(typingTimer);7});8function doneTyping () {9}10$('#myInput').keyup(function(){11 clearTimeout(typingTimer);12 typingTimer = setTimeout(doneTyping, doneTypingInterval);13});14$('#myInput').keydown(function(){15 clearTimeout(typingTimer);16});17function doneTyping () {18}19$('#myInput').keyup(function(){20 clearTimeout(typingTimer);21 typingTimer = setTimeout(doneTyping, doneTypingInterval);22});23$('#myInput').keydown(function(){24 clearTimeout(typingTimer);25});26function doneTyping () {27}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!