doc_frame_script.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /* globals addMessageListener, sendAsyncMessage */
  4. "use strict";
  5. // A helper frame-script for brower/devtools/animationinspector tests.
  6. /**
  7. * Toggle (play or pause) one of the animation players of a given node.
  8. * @param {Object} data
  9. * - {String} selector The CSS selector to get the node (can be a "super"
  10. * selector).
  11. * - {Number} animationIndex The index of the node's animationPlayers to play
  12. * or pause
  13. * - {Boolean} pause True to pause the animation, false to play.
  14. */
  15. addMessageListener("Test:ToggleAnimationPlayer", function (msg) {
  16. let {selector, animationIndex, pause} = msg.data;
  17. let node = superQuerySelector(selector);
  18. if (!node) {
  19. return;
  20. }
  21. let animation = node.getAnimations()[animationIndex];
  22. if (pause) {
  23. animation.pause();
  24. } else {
  25. animation.play();
  26. }
  27. sendAsyncMessage("Test:ToggleAnimationPlayer");
  28. });
  29. /**
  30. * Change the currentTime of one of the animation players of a given node.
  31. * @param {Object} data
  32. * - {String} selector The CSS selector to get the node (can be a "super"
  33. * selector).
  34. * - {Number} animationIndex The index of the node's animationPlayers to change.
  35. * - {Number} currentTime The current time to set.
  36. */
  37. addMessageListener("Test:SetAnimationPlayerCurrentTime", function (msg) {
  38. let {selector, animationIndex, currentTime} = msg.data;
  39. let node = superQuerySelector(selector);
  40. if (!node) {
  41. return;
  42. }
  43. let animation = node.getAnimations()[animationIndex];
  44. animation.currentTime = currentTime;
  45. sendAsyncMessage("Test:SetAnimationPlayerCurrentTime");
  46. });
  47. /**
  48. * Change the playbackRate of one of the animation players of a given node.
  49. * @param {Object} data
  50. * - {String} selector The CSS selector to get the node (can be a "super"
  51. * selector).
  52. * - {Number} animationIndex The index of the node's animationPlayers to change.
  53. * - {Number} playbackRate The rate to set.
  54. */
  55. addMessageListener("Test:SetAnimationPlayerPlaybackRate", function (msg) {
  56. let {selector, animationIndex, playbackRate} = msg.data;
  57. let node = superQuerySelector(selector);
  58. if (!node) {
  59. return;
  60. }
  61. let player = node.getAnimations()[animationIndex];
  62. player.playbackRate = playbackRate;
  63. sendAsyncMessage("Test:SetAnimationPlayerPlaybackRate");
  64. });
  65. /**
  66. * Get the current playState of an animation player on a given node.
  67. * @param {Object} data
  68. * - {String} selector The CSS selector to get the node (can be a "super"
  69. * selector).
  70. * - {Number} animationIndex The index of the node's animationPlayers to check
  71. */
  72. addMessageListener("Test:GetAnimationPlayerState", function (msg) {
  73. let {selector, animationIndex} = msg.data;
  74. let node = superQuerySelector(selector);
  75. if (!node) {
  76. return;
  77. }
  78. let animation = node.getAnimations()[animationIndex];
  79. animation.ready.then(() => {
  80. sendAsyncMessage("Test:GetAnimationPlayerState", animation.playState);
  81. });
  82. });
  83. /**
  84. * Like document.querySelector but can go into iframes too.
  85. * ".container iframe || .sub-container div" will first try to find the node
  86. * matched by ".container iframe" in the root document, then try to get the
  87. * content document inside it, and then try to match ".sub-container div" inside
  88. * this document.
  89. * Any selector coming before the || separator *MUST* match a frame node.
  90. * @param {String} superSelector.
  91. * @return {DOMNode} The node, or null if not found.
  92. */
  93. function superQuerySelector(superSelector, root = content.document) {
  94. let frameIndex = superSelector.indexOf("||");
  95. if (frameIndex === -1) {
  96. return root.querySelector(superSelector);
  97. }
  98. let rootSelector = superSelector.substring(0, frameIndex).trim();
  99. let childSelector = superSelector.substring(frameIndex + 2).trim();
  100. root = root.querySelector(rootSelector);
  101. if (!root || !root.contentWindow) {
  102. return null;
  103. }
  104. return superQuerySelector(childSelector, root.contentWindow.document);
  105. }