Home | History | Annotate | Download | only in actions
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 
      6 // This file provides common functions for media actions.
      7 window.__findMediaElements = function(selector) {
      8   // Returns elements matching the selector, otherwise returns the first video
      9   // or audio tag element that can be found.
     10   // If selector == 'all', returns all media elements.
     11   if (selector == 'all') {
     12     return document.querySelectorAll('video, audio');
     13   } else if (selector) {
     14     return document.querySelectorAll(selector);
     15   } else {
     16     var media = document.getElementsByTagName('video');
     17     if (media.length > 0) {
     18       return [media[0]];
     19     } else {
     20       media = document.getElementsByTagName('audio');
     21       if (media.length > 0) {
     22         return [media[0]];
     23       }
     24     }
     25   }
     26   console.error('Could not find any media elements matching: ' + selector);
     27   return [];
     28 };
     29 
     30 window.__hasEventCompleted = function(selector, event_name) {
     31   // Return true if the event_name fired for media satisfying the selector.
     32   var mediaElements = window.__findMediaElements(selector);
     33   for (var i = 0; i < mediaElements.length; i++) {
     34     if (!mediaElements[i][event_name + '_completed'])
     35       return false;
     36   }
     37   return true;
     38 };
     39