4chan \ IB Simple Web Media Player

Simple Web Media Player for 4chan and other imageboards.

当前为 2022-09-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 4chan \ IB Simple Web Media Player
  3. // @description Simple Web Media Player for 4chan and other imageboards.
  4. // @namespace LabMember-001
  5. // @author Hououin Kyōma
  6. // @license GPLv3
  7. // @version 1.4.8
  8.  
  9. // @grant none
  10. // @run-at document-end
  11.  
  12. // @match https://*.4chan.org/*
  13. // @match https://*.4channel.org/*
  14. // @match https://*.smuglo.li/*
  15. // @match https://*.smugloli.net/*
  16. // @match https://*.kissu.moe/*
  17. // @match https://*.4taba.net/*
  18. // @match https://*.2kind.moe/*
  19. // @match https://*.1chan.net/*
  20. // @match https://*.otterchat.net/*
  21. // @match https://*.fatchan.org/*
  22. // @match https://*.7chan.org/*
  23. // @match https://*.420chan.org/*
  24. // @match https://*.anon.cafe/*
  25. // @match https://*.4-ch.net/*
  26. // @match https://*.sushigirl.us/*
  27. // @match https://*.uboachan.net/*
  28.  
  29. // @match https://*.archived.moe/*
  30. // @match https://*.desuarchive.org/*
  31. // @match https://*.4plebs.org/*
  32. // @match https://*.warosu.org/*
  33.  
  34. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAAilBMVEX///8zGeTw8PArQKsXuQAiAv+kpKTu6tkAAADd3d0vEP8fHx9hTOF/bPjj3/97enb6+ffx7//v7+/y7+KUkof6+ftsWeDKwttYQP89If+Ec+5WR7ff398dHRv7+viom94+JuPBvrBKMuIODg3+/f1tbGl0cnZlUepjWK+NfPVPO9SIeej6+vtAQEBg9CwMAAAA3ElEQVRIx+3UxxKCMBCA4bUggahIt/de3v/1HEiICe44iwM3/pPM5jtgVsGuHLTku3TfUW03JJI8YlDFIYUMn6A1iSgEjFhdZGB0zw6Kjzkxhor09XrZQScvJ45eS8SXbXQp3Yv5WNftJ9VJejTWkkLscK2JE4nYuxVTHaL2T6kWMppZSO4NmUoymo4Byb+ekakgLioAAo5MBbGwJVcbKacykzD2BykjEjERkeiITD6oAilQs6T512/+KisvzM+17MqK5ffx5V9yZCrIfBGg4uUhU/lD9jhD4h4yfQMAvzcdsmcBygAAAABJRU5ErkJggg==
  35.  
  36. // ==/UserScript==
  37.  
  38. // Most of the sites above are to test scripts.
  39.  
  40. console.log('Loading IB Media Player.');
  41.  
  42. /* Repository: */
  43. /* https://github.com/LabMember-001/Simple-Web-Media-Player */
  44. /* https://labmember-001.github.io/Simple-Web-Media-Player/ */
  45. /* */
  46. /* */
  47. /* */
  48.  
  49. /* == BOARD SUPPORT ==
  50. // Script will alter behavior on different imageboard backends
  51. // When adding custom board scripts, alter board behavior on bottom of the script. You'll need some knowledge on JS.
  52. // In most cases, simply adding an @match to the top of this script to turn it on for that domain will work
  53. // as it captures all link presses and checks for webm.
  54. // If you want more compatability, check the issue tracker for existing requests and if there are none, you may request help.
  55. */
  56.  
  57. /* == SCRIPT CONFIGURATION ==
  58. // To define a board as a precoded backend type (vichan, tinyib, lynxchan, etc) simply
  59. // add a regex for that domain in the swmpBoards category below these comments.
  60. */
  61.  
  62. /* == TO DO ==
  63. //
  64. // High priority
  65. // - Resizing
  66. //
  67. // Medium Priority
  68. // - Loading poster for large files
  69. // - Make window move stop if you release mouseup outside of the screen.
  70. // - Automatic backend script detection.
  71. //
  72. // Low Priority
  73. // - Title length
  74. // - Fix support for archived.moe redirection urls.
  75. // - Fix titles on some backends. (Mostly just vichan+4chan taking original filename right now).
  76. */
  77.  
  78. /* == Current Script Support ==
  79. // Yotsuba (4chan)
  80. // Tinyboard/Vichan/Infinity
  81. // Fuuka
  82. // TinyIB
  83. // Wakaba
  84. // FoolFuuka (Not archived.moe's redirects, but 4plebs is fine).
  85. // Kusaba
  86. */
  87.  
  88. /* == Script Specific Bugs ==
  89. // - Vichan shows original title when clicked on video thumb, but not link.
  90. // - Doesnt grab titles on Fuuka, TinyIB, Wakaba, FoolFuuka
  91. //
  92. */
  93.  
  94. /*
  95. // == Missing Script Support ==
  96. // Some Kusaba sites have issues loading video when clicked on thumbnail. Links typically work. 7chan is fine.
  97. // Phutaba has a slight bug with videos which shows file information when hovering over player depending on which link you click.
  98. // Some of the bigger Lynxchan sites are very modified and have no thumbnail click support.
  99. // jschan and vichan will not properly work if not added to the board specific types. Should be fixable for me later.
  100. // InfinityNext does not work. Don't care.
  101. */
  102.  
  103. // Get domain
  104. var currentUrl = window.location.href;
  105.  
  106. // Board Types - probably temporary.
  107. var swmpBoards = {
  108. fourchan: // For title support, otherwise works well without adding here.
  109. ['https:\/\/*..*(4chan|4channel).org\/*'],
  110. vichan: // For Title and thumbnail click support. Not great without due to thumbnails.
  111. ['https:\/\/*..*(smuglo.li|smugloli.net)\/*',
  112. 'https:\/\/*..*2kind.moe\/*',
  113. 'https:\/\/*..*kissu.moe\/*',
  114. 'https:\/\/*..*sushigirl.us\/*',
  115. 'https:\/\/*..*uboachan.net\/*'],
  116. tinyib: // Title support not written, otherwise works well without adding here.
  117. ['https:\/\/*..*1chan.net\/*'],
  118. wakaba: // Title support not written, otherwise works well without adding here.
  119. ['https:\/\/*..*otterchat.net\/*'],
  120. lynxchan: // Title support not written, otherwise works well without adding here.
  121. ['https:\/\/*..*anon.cafe\/*'],
  122. jschan: // Title support not written. Jschan installations *must* be added here to work properly.
  123. ['https:\/\/*..*fatchan.org\/*']
  124. }
  125.  
  126. // regex to check the backend script of current domain
  127. var backendScript = [];
  128. Object.keys(swmpBoards).forEach(script => {
  129. swmpBoards[script].forEach(regex => {
  130. if (currentUrl.match(regex)) {
  131. backendScript = script;
  132. }
  133. });
  134. });
  135.  
  136. console.log(backendScript);
  137.  
  138.  
  139. // This configuration variable can be overwritten wherever you want later
  140. // on as you wish or add your own site variables for user configuration.
  141. var swmpConfig = {
  142. autoplay: 'true', // Autoplay media when launched by SWMP.
  143. loop: 'true', // Loop media when launched by SWMP.
  144. windowed: 'true',
  145. positionTop: '100',
  146. positionOffset: '100',
  147. positionSide: 'right',
  148. volume: 60,
  149. volumeScroll: 'true',
  150. muted: 'false', //Not implemented
  151. skip: 5, //skip forward/backward keyboard shortcut, seconds.
  152. theme: 'default', //Default theme
  153. themes: //All themes
  154. [
  155. ['default', 'MPC Light'],
  156. ['dark', 'MPC Dark'],
  157. ['kurisu', 'Kurisumasu'],
  158. ['winxp', 'WIN_XP'],
  159. ['modernity', 'Modernity']
  160. ],
  161. files: 'avi|mpeg|mpg|ogv|mp4|webm|flv|wav|mp3|m4a|mp2|ogg|flac',
  162. allowMultiple: 'false',
  163. downloadAttribute: 'true', //true = override default action, false = download
  164. doubleclickMaximize: 'false' //true = doubleclick video to maximize instead of fullscreen
  165. }
  166.  
  167. // Initialize site configuration. As of now it's on a per site basis and not using GM values, I like different designs.
  168. if (localStorage.swmpVolume == undefined) {
  169. localStorage.swmpVolume = swmpConfig.volume;
  170. } else {
  171. swmpConfig.volume = localStorage.swmpVolume;
  172. }
  173.  
  174. if (localStorage.swmpTheme == undefined) {
  175. localStorage.swmpTheme = swmpConfig.theme;
  176. } else {
  177. swmpConfig.theme = localStorage.swmpTheme;
  178. }
  179.  
  180. if (localStorage.swmpAutoplay == undefined) {
  181. localStorage.swmpAutoplay = swmpConfig.autoplay;
  182. } else {
  183. swmpConfig.autoplay = localStorage.swmpAutoplay;
  184. }
  185.  
  186. if (localStorage.swmpLoop == undefined) {
  187. localStorage.swmpLoop = swmpConfig.loop;
  188. } else {
  189. swmpConfig.loop = localStorage.swmpLoop;
  190. }
  191.  
  192. if (localStorage.swmpAllowMultiple == undefined) {
  193. localStorage.swmpAllowMultiple = swmpConfig.allowMultiple;
  194. } else {
  195. swmpConfig.allowMultiple = localStorage.swmpAllowMultiple;
  196. }
  197.  
  198. if (localStorage.swmpWindowed == undefined) {
  199. localStorage.swmpWindowed = swmpConfig.windowed;
  200. } else {
  201. swmpConfig.windowed = localStorage.swmpWindowed;
  202. }
  203.  
  204. if (localStorage.swmpMuted == undefined) {
  205. localStorage.swmpMuted = swmpConfig.muted;
  206. } else {
  207. swmpConfig.muted = localStorage.swmpMuted;
  208. }
  209.  
  210. if (localStorage.swmpVolumeScroll == undefined) {
  211. localStorage.swmpVolumeScroll = swmpConfig.volumeScroll;
  212. } else {
  213. swmpConfig.volumeScroll = localStorage.swmpVolumeScroll;
  214. }
  215.  
  216. if (localStorage.swmpDownloadAttribute == undefined) {
  217. localStorage.swmpDownloadAttribute = swmpConfig.downloadAttribute;
  218. } else {
  219. swmpConfig.downloadAttribute = localStorage.swmpDownloadAttribute;
  220. }
  221.  
  222. if (localStorage.swmpDoubleclickMaximize == undefined) {
  223. localStorage.swmpDoubleclickMaximize = swmpConfig.doubleclickMaximize;
  224. } else {
  225. swmpConfig.doubleclickMaximize = localStorage.swmpDoubleclickMaximize;
  226. }
  227.  
  228.  
  229. // Add style to head when DOM is loaded.
  230.  
  231. if (!document.getElementById('swmp-stylesheet')) { // Don't bother injecting style on demo page.
  232. var swmpStyle = document.createElement('style');
  233. swmpStyle.setAttribute('id', 'swmp-stylesheet');
  234. swmpStyle.innerHTML = `div.swmp,div.swmp.swmp-theme-dark,div.swmp.swmp-theme-dark *{--swmp-container-border:#000;--swmp-controls-background:var(--swmp-background);--swmp-button-background:var(--swmp-background);--swmp-seek-height:30px;--swmp-seek-offset:10px;--swmp-btn-border-right:#000;--swmp-btn-border-bottom:#000;--swmp-btn-border-left-active:#000;--swmp-btn-border-top-active:#000}.swmp *,div.swmp.swmp-container{font-family:-apple-system,BlinkMacSystemFont,URW Gothic,MS PGothic,Helvetica,sans-serif;font-size:11pt;text-indent:4px;letter-spacing:1px;line-height:1;outline:0;color:var(--swmp-text-color)}div.swmp iframe,div.swmp video{max-width:500px;max-height:500px;width:auto;height:auto;margin:auto}.swmp *,div.swmp.swmp-container,select.swmp.swmp-selector{color:var(--swmp-text-color)}div.swmp,div.swmp.swmp-theme-dark,div.swmp.swmp-theme-dark *,div.swmp.swmp-theme-winxp,div.swmp.swmp-theme-winxp *{--swmp-range-thumb-border-right:#000;--swmp-range-thumb-border-bottom:#000}.swmp *{background:0 0;border:0;margin:0;padding:0;height:unset;width:unset}div.swmp{--swmp-background:#e6e6e6;--swmp-container-border-radius:0;--swmp-container-box-shadow:none;--swmp-player-container-background:#000;--swmp-player-container-border-left:#000;--swmp-player-container-border-top:#000;--swmp-player-container-border-right:#000;--swmp-player-container-border-bottom:#000;--swmp-settings-background:var(--swmp-controls-background);--swmp-text-color:#000;--swmp-button-mask-color:#000;--swmp-seek-background:var(--swmp-controls-background);--swmp-seek-progress-color:lightgrey;--swmp-seek-border-left:darkgray;--swmp-seek-border-top:darkgray;--swmp-seek-border-right:#fff;--swmp-seek-border-bottom:#fff;--swmp-range-thumb-color:var(--swmp-controls-background);--swmp-range-thumb-border-left:#fff;--swmp-range-thumb-border-top:#fff;--swmp-btn-border-radius:0;--swmp-btn-border-left:#fff;--swmp-btn-border-top:#fff;--swmp-btn-border-right-active:#fff;--swmp-btn-border-bottom-active:#fff}div.swmp.swmp-theme-dark,div.swmp.swmp-theme-dark *{--swmp-background:#333;--swmp-text-color:#888;--swmp-button-mask-color:#888;--swmp-seek-progress-color:#555;--swmp-seek-border-left:#1a1a1a;--swmp-seek-border-top:#1a1a1a;--swmp-seek-border-right:#464646;--swmp-seek-border-bottom:#464646;--swmp-range-thumb-color:var(--swmp-background);--swmp-range-thumb-border-left:#777;--swmp-range-thumb-border-top:#777;--swmp-btn-border-left:#777;--swmp-btn-border-top:#777;--swmp-btn-border-right-active:#777;--swmp-btn-border-bottom-active:#777}div.swmp.swmp-theme-kurisu,div.swmp.swmp-theme-kurisu *{--swmp-background:#a44242;--swmp-text-color:#fff;--swmp-button-mask-color:#fff}div.swmp.swmp-container{position:relative;display:inline-flex;flex-direction:column;padding:2px;background:var(--swmp-background);border:1px solid var(--swmp-container-border);border-radius:var(--swmp-container-border-radius);z-index:100;overflow:hidden;min-width:320px;width:auto;box-shadow:var(--swmp-container-box-shadow)}div.swmp .swmp-player-container,div.swmp audio,div.swmp iframe,div.swmp video{background:var(--swmp-player-container-background)}div.swmp.swmp-window.swmp-window-container{display:flex;justify-content:space-between;padding-bottom:2px}div.swmp.swmp-fullscreen div.swmp-settings.swmp-settings-container,div.swmp.swmp-fullscreen div.swmp-window.swmp-window-container,div.swmp.swmp-minimized div.swmp-player-container{display:none}div.swmp.swmp-fullscreen div.swmp-player-container{display:block;width:100vw;height:100vh}span.swmp.swmp-window.swmp-window-titlebar{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;-webkit-user-select:none;user-select:none;position:relative;margin:auto;cursor:move;width:100%;text-align:center;display:block}span.swmp.swmp-window.swmp-window-title{display:block;max-width:260px;text-overflow:ellipsis;overflow:hidden;margin:auto auto -2px;line-height:1.5}span.swmp.swmp-window.swmp-window-buttons-contain{display:flex;flex:0 1 auto;margin-right:-2px}div.swmp .swmp-player-container{display:flex;position:relative;height:100%;border:1px solid;border-left-color:var(--swmp-player-container-border-left);border-top-color:var(--swmp-player-container-border-top);border-right-color:var(--swmp-player-container-border-right);border-bottom-color:var(--swmp-player-container-border-bottom)}div.swmp.swmp-audio div.swmp-player-container,div.swmp.swmp-fullscreen div.swmp-player-container{border:none}div.swmp.swmp-container.swmp-fullscreen{background:#000;position:unset!important;width:100%;height:auto;border-radius:0!important}.swmp.swmp-container.swmp-maximized{position:fixed!important;top:0!important;bottom:0!important;right:0!important;left:0!important;transition:.2s ease-out;border-radius:0!important}div.swmp.swmp-container.swmp-maximized audio,div.swmp.swmp-container.swmp-maximized iframe,div.swmp.swmp-container.swmp-maximized video{position:absolute;max-width:100%;max-height:100%;width:100%;height:100%;top:0;bottom:0;left:0;right:0}div.swmp.swmp-container.swmp-fullscreen audio,div.swmp.swmp-container.swmp-fullscreen iframe,div.swmp.swmp-container.swmp-fullscreen video{position:relative;width:100%;height:100%;max-width:100%;max-height:100%}div.swmp iframe{min-width:420px;min-height:236px;pointer-events:none}div.swmp audio{min-width:320px;min-height:40px}div.swmp.swmp-controls{bottom:0;left:0;background:var(--swmp-controls-background);width:100%;display:flex;flex-direction:column}div.swmp.swmp-settings-container{background:var(--swmp-settings-background);display:flex;flex-direction:column;align-items:flex-start}div.swmp.swmp-fullscreen div.swmp.swmp-controls{position:absolute;opacity:0;transition:opacity .5s ease-out}div.swmp.swmp-fullscreen.swmp-movingmouse div.swmp.swmp-controls{opacity:1;position:absolute;transition:none}div.swmp.swmp-container .swmp-player-container{cursor:none}div.swmp.swmp-movingmouse .swmp-player-container{cursor:unset}div.swmp.swmp-controls span.swmp-seek-container{display:flex;width:calc(100% - 6px);height:var(--swmp-seek-height);margin:auto;position:relative}div.swmp.swmp-controls span.swmp-seek-container input.swmp.swmp-seeker{width:calc(100% - var(--swmp-seek-offset));left:calc(var(--swmp-seek-offset)/ 2);-webkit-appearance:none;background:#0000;padding:0;margin:0;height:var(--swmp-seek-height);position:absolute;z-index:1;cursor:pointer;-webkit-margin-top:-14px}input[type=range].swmp{background:0 0!important;border:0!important;outline:0!important}span.swmp input[type=range]::-webkit-slider-runnable-track{width:100%;height:8px;cursor:pointer;background:#0000;border-radius:0;border:1px solid #000;border-left-color:var(--swmp-seek-border-left);border-top-color:var(--swmp-seek-border-top);border-right-color:var(--swmp-seek-border-right);border-bottom-color:var(--swmp-seek-border-bottom)}span.swmp input[type=range]::-moz-range-track{width:100%;height:6px;cursor:pointer;background:#0000;border-radius:0;border:1px solid #000;border-left-color:var(--swmp-seek-border-left);border-top-color:var(--swmp-seek-border-top);border-right-color:var(--swmp-seek-border-right);border-bottom-color:var(--swmp-seek-border-bottom)}span.swmp progress::-webkit-progress-bar{background:#0000}span.swmp progress::-webkit-progress-value{background:var(--swmp-seek-progress-color)}span.swmp progress::-moz-progress-bar{background:var(--swmp-seek-progress-color)}span.swmp input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;border:4px solid var(--swmp-range-thumb-color);height:8px;width:2px;border-radius:0;padding:4px 3px;background:#0000;background:linear-gradient(180deg,var(--swmp-seek-border-top) 10%,var(--swmp-seek-background) 10%,var(--swmp-seek-background) 90%,var(--swmp-seek-border-bottom) 90%);cursor:pointer;margin-top:-6px;box-shadow:1px 1px 0 0 var(--swmp-range-thumb-border-bottom),-1px -1px 0 0 var(--swmp-range-thumb-border-top)}span.swmp input[type=range]::-moz-range-thumb{appearance:none;border:4px solid var(--swmp-range-thumb-color);height:8px;width:6px;border-radius:0;background:linear-gradient(180deg,var(--swmp-seek-border-top) 10%,var(--swmp-seek-background) 10%,var(--swmp-seek-background) 90%,var(--swmp-seek-border-bottom) 90%);cursor:pointer;box-shadow:1px 1px 0 0 var(--swmp-range-thumb-border-bottom),-1px -1px 0 0 var(--swmp-range-thumb-border-top)}span.swmp input[type=range]::-webkit-range-progress{height:6px;background-color:#0000}span.swmp input[type=range]::-moz-range-progress{height:6px;background-color:#0000}span.swmp progress.swmp-volume{width:50px;position:absolute;height:6px;right:8px;border:none;bottom:7px;z-index:0;background:var(--swmp-seek-background)}span.swmp input.swmp-volume[type=range]{-webkit-appearance:none;background:#0000;padding:0;margin-left:2px;cursor:pointer;position:relative}span.swmp input.swmp-volume[type=range]::-webkit-slider-thumb{padding:4px 2px;width:2px}span.swmp input.swmp-volume[type=range]::-moz-range-thumb{padding:0 1px;width:1px}div.swmp.swmp-controls span.swmp-seek-container progress.swmp-progress{width:calc(100% - var(--swmp-seek-offset));left:calc(var(--swmp-seek-offset)/ 2);height:8px;z-index:0;position:relative;background:var(--swmp-seek-background);border:none;margin:auto 0}span.swmp.swmp-row-bottom{display:flex;flex-direction:row;height:20px;margin-bottom:2px}div.swmp.swmp-fullscreen span.swmp.swmp-row-bottom{padding-bottom:5px}span.swmp.swmp-buttons-container{height:100%}select.swmp.swmp-selector{-webkit-appearance:none;appearance:none;background:var(--swmp-button-background);border:1px solid var(--swmp-text-color);outline:0;border-radius:0;width:85px;overflow:hidden;text-overflow:ellipsis;letter-spacing:0;text-indent:0}label.swmp.swmp-settings{display:inline-flex;flex-direction:row-reverse;line-height:1.2}input.swmp.swmp-settings{margin:-2px 0 0 4px;border:1px solid var(--swmp-text-color);appearance:none;-webkit-appearance:none;outline:0;width:14px;height:14px;background:var(--swmp-controls-background)}input.swmp.swmp-settings:checked{outline:5px inset var(--swmp-text-color);outline-offset:-8px}button.swmp.swmp-button{min-width:26px;height:100%;padding:0 4px;display:inline-block;cursor:pointer;margin:0 2px;color:var(--swmp-button-mask-color);background:var(--swmp-button-background);border:1px solid;border-radius:var(--swmp-btn-border-radius);border-bottom-color:var(--swmp-btn-border-bottom);border-right-color:var(--swmp-btn-border-right);border-top-color:var(--swmp-btn-border-top);border-left-color:var(--swmp-btn-border-left);filter:unset}button.swmp.swmp-button span{display:block;width:16px;height:16px;image-rendering:crisp-edges;image-rendering:pixelated;background-repeat:no-repeat;background-color:var(--swmp-button-mask-color);-webkit-mask-image:var(--image);mask-image:var(--image);-webkit-mask-size:16px;mask-size:16px;-webkit-mask-repeat:no-repeat;mask-repeat:no-repeat}button.swmp.swmp-button:active span{transform:translateX(1px) translateY(1px)}span.swmp.swmp-window button{height:20px;min-width:22px;width:22px}button.swmp.swmp-button.swmp-playlist span{--image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAgMAAAAqbBEUAAAADFBMVEUAAAAAAAAAAAAAAAA16TeWAAAABHRSTlMARP/Eyy24XQAAAHxJREFUeJzN0MENgCAMBVBkHKdgHqdhCGLCBxMODsAIxomkLQca4h0OlBcaaGvMWsvRtvFxN5mCLQJ4ApwgMLwAlZA6bgIeQbtqSB0XoSUw4AixIxNQBImRBTh5hwC/UGkvP3PMT4+fqnLGQiO3EObmVNvjQNSoONgqWGt96g97WXTz3RYAAAAASUVORK5CYII=)}button.swmp.swmp-button.swmp-playlist.swmp-playlist-prev span{transform:rotate(180deg)}button.swmp.swmp-button.swmp-window-minimize span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAARElEQVRYR+3V0Q0AMAQFQPYfuv7apAPg4yzguQQZw5XD/UMAAgQIECCwTuA0fcc7+C8gAIFxgaYleG3W3QECBAgQaBcokVQGIRA6KiEAAAAASUVORK5CYII=');margin-top:2px;-webkit-mask-size:12px;mask-size:12px}button.swmp.swmp-button.swmp-window-maximize span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAANUlEQVQ4T2NkoBAwQvX/J9McRqobADOQkINgLsZwwagBDAyjYUCFMCCUAtHlqZcXSLUZrh4AU/cYEVH9Rd0AAAAASUVORK5CYII=');margin-top:2px;-webkit-mask-size:12px;mask-size:12px}button.swmp.swmp-button.swmp-window-close span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA9UlEQVRoge2Y2woCMRBD5699dB79a7ViQfDWaZNMFybQR5NzVndhNatUKpUjxe/nctT+Vn59HsYItf+1nDFC7f9Ujhyh9v8qR4xQ+0fKV0bY/XYKDERHIvDtnGcEWBIyeIaEHB4pkQaPkEiHnwXxyc9QE/0mtrjyCgkZfA9SQg7fg5BIg+9ZkUiH75mR2Aa+xS0uwHyzC8Vt/ieULuG2fhOnSfgA3LYSDoBOk2DAyySi8O1RyXyzo8P3pEuswKdLIODTJJDwcgkGvFQiMhKBV/UPj0yXC/r/jiyXC/q/jsDKBf1vI/ByQf8jbtz/bdj9lUqlAs4N+1iFrUSwCpcAAAAASUVORK5CYII=');margin-top:2px;-webkit-mask-size:12px;mask-size:12px}button.swmp.swmp-button.swmp-playbutton span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAA8klEQVRoQ+3aMQ6DMAyF4XDycjCGVmrPBRmapVLkJLafn1GRMiHB/+EpEVtJfm3J+8vtAO86kb2uI8tkfidwfsM/WSA9QBsAPUQC0ENGAbSQWQAdZBVAA9ECwiFWgDCINQAO8QLAIN4AdwgK4AZBA8whUQAzSDRADWEBLEPYANMQVsAwhB0gQrIAupA/AHR60d2bs09APFRgBYjhbfJsgOFwNsB0OAtgOTwaoA6PApiFowHm4SiAW7g3wD3cCwALtwbAw60AYeFaQHj4KoAmfBZAFz4KoA2XAPThPcCz3njU9QJtFdWvud2vBuovgn5A+glcJSF8MQrukbIAAAAASUVORK5CYII=')}button.swmp.swmp-button.swmp-playbutton.swmp-playing span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAtUlEQVRoQ+2Y2w6AIAxD5f8/2kuCRgiJbdjDMMfnxUE5o9WyLf6Uxde//W4D+8SJuGKE9OqbhrxUFCGkFxt4qQ1CInpX2SMWCIGQwc1ILBACIRBqFQixd1HUkF4MMUMs8taVESVGupFGDZpACIROBTCy5tOsMhGiijiMIb2IEkQJkTeihCAUUUIQ6S4hShAliBKVAZwYJzbuTn7ufoiFExs04cRpndg4xRyl7uDlWPWE+aTbwAFy3FQxPpmarQAAAABJRU5ErkJggg==')}button.swmp.swmp-button.swmp-stopbutton span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAUUlEQVRYR+3Xuw0AIAwD0WQC2H9LJuDXICFqm+KywEWvc4b50tyP7x7oIpE6O221bgEeQAABBBBAAAEEEEAAAQTsAkW0jPYqei0jUf9k7ON0AGFsNSFlb3+JAAAAAElFTkSuQmCC')}.swmp.swmp-timer-container,button.swmp.swmp-button.swmp-fullscreen{margin-left:auto}button.swmp.swmp-button.swmp-fullscreen span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAP0lEQVQ4T2NkoBAwQvX/x2EOQXmCCghZgG4AjE/IYzAXMw4jAwj5Gac8sYFG0AB4qBLplOEYCwOfF4gMfExlADQ3GBE+X9RsAAAAAElFTkSuQmCC')}button.swmp.swmp-button.swmp-volume span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAG1BMVEVHcEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABp4cHsAAAACHRSTlMA1WZ/JZz0Rme5O54AAACGSURBVDjL5ZIxCoAwDEWjVXDs6OjUWXDxRl6hqyL2H9uqKKL5oKtmzIOXNvkiP6qc9LOSAAciAoiIAAcdRNEGmrtoBQb1TbSCFt3eK3CUSAr0KliUlQbEA1YFCTCqID47qMAAwztAVXS4x2RfffC8kusSw7O100Px0/Iw0PjwwPGI0lB/s2bRbW7duVgj2wAAAABJRU5ErkJggg==')}button.swmp.swmp-button.swmp-volume.swmp-min span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAQAAAD9CzEMAAAAd0lEQVR42u2Xuw2AMAwFr41YhKmyVyp2YhZ6pwEJqJDgmRTv3PukV/gDxpjBKDRl+5mV0LWvbIRKUGjEXrJoZIIjGongHI1AcI3mc8E9mpeCeFwWWGCBBTqBfNj9MK4TFk7CykxZ+glnS8LhlXI6Akws/gCMGZAO8wpmVouK9vcAAAAASUVORK5CYII=')}button.swmp.swmp-button.swmp-volume.swmp-max span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAD1BMVEVHcEwAAAAAAAAAAAAAAADTrAj/AAAABXRSTlMA/4hD1KmiQHMAAACwSURBVDjL1ZPBDYMwDEV/nQzAIwwAVQdoNoD9l+qhgAyKW/VGfbHkp/wk9rf0//EI6sZ8Kqx5oj/U07IdgKcHlXfOcNAyVpCA4oU2oAqDF9pB9pcYK7hLAkYnBJKMUZqgc0IgaaJIFYp0Yw/JYJAy9CegBUZZA1SYlWA4gwxdExgUCWiA/mfQlgovj56bog/6loRN/Nz2w6CSG1Q82tgMoX1iw8UWDU0dr8HXxblwvAAGdxy1HX87LAAAAABJRU5ErkJggg==')}button.swmp.swmp-button.swmp-volume.swmp-mute span{--image:url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAHlBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC3KG9qAAAACnRSTlMACZko0v9e9AufnFf4OQAAAJdJREFUeJzlkrsNgDAMRCMhL8BO6WluBmZha2LZDvmdRA1uUHi+S/xJ6Uexk/9bJuAEMcIabBmQQNLknFBw2KERFyMFcEEFalROMAlCaUYFWKoLBDU81wUdkCobgGbHDT0Q/06gSOJJA3hqeKmgd7BX0TpY5V2v2ibO3bW2L+YRg5on6KOtRT9AzdIy2PrwheMrmi4GPhk3sCZY/9usUnQAAAAASUVORK5CYII=')}span.swmp.swmp-volume-container{display:flex;width:60px;height:100%;position:relative}span.swmp.swmp-volume-container input.swmp.swmp-volume.swmp-range{width:50px;vertical-align:top;position:relative}button.swmp.swmp-button:active{border-bottom-color:var(--swmp-btn-border-bottom-active);border-right-color:var(--swmp-btn-border-right-active);border-top-color:var(--swmp-btn-border-top-active);border-left-color:var(--swmp-btn-border-left-active)}span.swmp.swmp-timer-container{display:inline-table;margin-right:5px;cursor:default}span.swmp.swmp-timer-container span.swmp.swmp-time{display:table-cell;vertical-align:bottom}#quote-preview{z-index:1}div.swmp.swmp-theme-modernity,div.swmp.swmp-theme-modernity *{--swmp-background:#222;--swmp-container-border:#222;--swmp-text-color:#eee;--swmp-button-mask-color:#eee;--swmp-controls-background:#222;--swmp-btn-border-left:#0000;--swmp-btn-border-top:#0000;--swmp-btn-border-right:#0000;--swmp-btn-border-bottom:#0000;--swmp-btn-border-left-active:#0000;--swmp-btn-border-top-active:#0000;--swmp-btn-border-right-active:#0000;--swmp-btn-border-bottom-active:#0000;--swmp-seek-background:#444;--swmp-seek-progress-color:#bbb;--swmp-seek-border-left:#444;--swmp-seek-border-top:#444;--swmp-seek-border-right:#444;--swmp-seek-border-bottom:#444;--swmp-container-box-shadow:0px 0px 10px #000;font-family:Arial;font-weight:700}div.swmp.swmp-theme-modernity input[type=range]::-moz-range-thumb{border:none;height:8px;width:8px;background:#eee;box-shadow:none;border-radius:100%;padding:4px}div.swmp.swmp-theme-modernity input[type=range]::-webkit-slider-thumb{border:none;height:8px;width:8px;background:#eee;box-shadow:none;border-radius:100%;padding:8px}div.swmp.swmp-theme-winxp,div.swmp.swmp-theme-winxp *{--swmp-background:#0253d2;--swmp-container-border:none;--swmp-container-border-radius:3px;--swmp-container-box-shadow:#00000052 0px 0px 1px 0px;--swmp-controls-background:#ece9d8;--swmp-button-background:#fff;--swmp-seek-background:#ddd;--swmp-seek-progress-color:#17b900;--swmp-seek-border-left:#a4a4a4;--swmp-seek-border-top:#a4a4a4;--swmp-seek-border-right:#fff;--swmp-seek-border-bottom:#fff;--swmp-range-thumb-color:#fff;--swmp-range-thumb-border-left:#fff;--swmp-range-thumb-border-top:#3319e4;--swmp-btn-border-radius:3px;--swmp-btn-border-left:#2202ff;--swmp-btn-border-top:#2202ff;--swmp-btn-border-right:#000;--swmp-btn-border-bottom:#000;--swmp-btn-border-left-active:#000;--swmp-btn-border-top-active:#000;--swmp-btn-border-right-active:#000;--swmp-btn-border-bottom-active:#000}div.swmp.swmp-theme-winxp .swmp-window-title{color:#fff;text-shadow:1px 1px #0f1089}div.swmp.swmp-theme-winxp .swmp.swmp-window.swmp-window-container{background:linear-gradient(180deg,#0684df,#0347c6 8%,#0240bb 40%,#0451c6 88%,#0253cc 93%,#0149cc 95%,#0230a4 100%)}div.swmp.swmp-theme-winxp button.swmp.swmp-button:hover{box-shadow:inset 0 0 2px 0 #ce700b}div.swmp.swmp-theme-winxp button.swmp.swmp-button:active{box-shadow:inset 0 0 2px 0 #074faa}div.swmp.swmp-theme-winxp .swmp-window button.swmp-button{background:linear-gradient(#1690e8,#2969e3 8%,#0438bd 100%);box-shadow:none!important;border-color:#fff}div.swmp.swmp-theme-winxp .swmp-window button.swmp-button:hover{background:linear-gradient(#2295e8,#3674ec 8%,#0b3fc6 100%)}div.swmp.swmp-theme-winxp .swmp-window button.swmp-button:active{background:linear-gradient(#0c74bf,#144cb7 8%,#0438bd 100%)}div.swmp.swmp-theme-winxp .swmp-window button.swmp-button span{background-color:#fff}div.swmp.swmp-theme-winxp .swmp-window button.swmp-button.swmp-window-close{background:linear-gradient(#ea4646,#e65a5a 8%,#d22828 100%)}div.swmp.swmp-theme-winxp .swmp-window button.swmp-button.swmp-window-close:hover{background:linear-gradient(#ea5050,#e66a6a 8%,#db3434 100%)}div.swmp.swmp-theme-winxp .swmp-window button.swmp-button.swmp-window-close:active{background:linear-gradient(#d22b2b,#c33838 8%,#d22828 100%)}div.swmp.swmp-theme-winxp span.swmp input[type=range]::-moz-range-thumb{border:1px solid #000;border-radius:2px 2px 4px 4px!important;height:16px;width:10px;box-shadow:inset 1px 1px 0 0 #fff,inset -1px -1px 0 0 #4d85dd;background:linear-gradient(180deg,#a7c8d9 20%,#fff 20%,#fff 80%,#a7c8d9 80%)}div.swmp.swmp-theme-winxp span.swmp input[type=range]::-webkit-slider-thumb{border:1px solid #000;border-radius:2px 2px 4px 4px!important;height:18px;width:12px;box-shadow:inset 1px 1px 0 0 #fff,inset -1px -1px 0 0 #4d85dd;background:linear-gradient(180deg,#a7c8d9 20%,#fff 20%,#fff 80%,#a7c8d9 80%)}div.swmp.swmp-theme-winxp span.swmp input.swmp-volume[type=range]::-moz-range-thumb{width:6px}div.swmp.swmp-theme-winxp span.swmp input.swmp-volume[type=range]::-webkit-slider-thumb{width:10px}div.swmp.swmp-theme-winxp span.swmp input:hover[type=range]::-moz-range-thumb{background:linear-gradient(180deg,#d9cca7 20%,#fff 20%,#fff 80%,#d9cca7 80%);box-shadow:inset 1px 1px 0 0 #fff,inset -1px -1px 0 0 #e9967a}div.swmp.swmp-theme-winxp span.swmp input:hover[type=range]::-webkit-slider-thumb{background:linear-gradient(180deg,#d9cca7 20%,#fff 20%,#fff 80%,#d9cca7 80%);box-shadow:inset 1px 1px 0 0 #fff,inset -1px -1px 0 0 #e9967a}div.swmp.swmp-theme-winxp span.swmp input:active[type=range]::-moz-range-thumb{box-shadow:inset 1px 1px 0 0 #fff,inset -1px -1px 0 0 orange;background:linear-gradient(180deg,orange 20%,#fff 20%,#fff 80%,#ff8c00 80%)}div.swmp.swmp-theme-winxp span.swmp input:active[type=range]::-webkit-slider-thumb{box-shadow:inset 1px 1px 0 0 #fff,inset -1px -1px 0 0 orange;background:linear-gradient(180deg,orange 20%,#fff 20%,#fff 80%,#ff8c00 80%)}@media screen and (max-width:480px){div.swmp.swmp-container{top:5px!important;right:5px!important;left:5px!important;margin-bottom:10px}div.swmp.swmp-container.swmp-maximized{margin:0}div.swmp audio,div.swmp iframe,div.swmp video{max-width:100%!important;max-height:100%!important;min-width:unset;min-height:unset;width:100%;height:100%}span.swmp.swmp-volume-container{display:none}}`;
  235. document.head.appendChild(swmpStyle);
  236. }
  237.  
  238. var youtubeIsLoaded = false;
  239. var gmwindow;
  240. if (typeof GM_info != 'undefined') {
  241. gmwindow = unsafeWindow;
  242. } else {
  243. gmwindow = window;
  244. }
  245.  
  246. var fileregex = new RegExp(`\.(${swmpConfig.files})+$`, 'gmi');
  247. var ytregex = new RegExp("^(?:https?:)?//[^/]*(?:youtube(?:-nocookie)?\.com|youtu\.be|yewtu\.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)", "gmi");
  248.  
  249. // SWMP Code
  250.  
  251. class swmp {
  252.  
  253. constructor(obj) {
  254. this.name = 'Simple Web Media Player';
  255. if (obj.id == undefined) {
  256. this.id = this.uuid();
  257. } else {
  258. this.id = obj.id;
  259. }
  260.  
  261. this.type = obj.type; // "video" or "audio" player. Youtube too.
  262. this.mime = obj.mime; // MIME type for source, Example: "video/webm". Feed to enable checking for file support (webm not supported by iOS)
  263. this.url = obj.url; // File Location
  264. this.poster = obj.poster; // Optional Video Preview for when autoplay is off.
  265. this.autoplay = obj.autoplay; // Optional Autoplay
  266. this.loop = obj.loop; // Optional Loop
  267. this.windowed = obj.windowed; // Optional set false to disable windowed mode and place inline
  268. this.defaultVolume = parseInt(swmpConfig.volume);
  269.  
  270. this.playlistLocation = 0;
  271. this.playlist = obj.playlist;
  272.  
  273. if (obj.url == undefined) {
  274. console.log('No media given');
  275. return false;
  276. }
  277.  
  278. if (obj.windowed == undefined) {
  279. this.windowed = swmpConfig.windowed;
  280. }
  281.  
  282. if (obj.title == undefined) {
  283. this.title = this.getFileName(obj.url);
  284. } else {
  285. this.title = obj.title; // If Applicable, assign from existing parameters on IB.
  286. }
  287.  
  288. // Check URL for Type and Mime, also sets mime+type based on fileextension in url
  289. if (this.checkURL(this.url) == false) {
  290. return false;
  291. }
  292.  
  293. // Create Container
  294. this.container = document.createElement('div');
  295. this.container.setAttribute('class', 'swmp swmp-container');
  296. this.container.setAttribute('id', this.id);
  297. this.container.setAttribute('tabindex', '0');
  298.  
  299. // Create Window Container
  300. this.prepareWindow();
  301.  
  302. // Prepare the media that's gonna play.
  303.  
  304. if (this.type == 'video' || this.type == 'audio') {
  305. // Check if browser can play formats, create audio or video tag and fill with source.
  306. this.preparePlayer();
  307. } else if (this.type == 'youtube') {
  308. // Load Youtube iframe.
  309. if (this.prepareYoutube() == false) {
  310. console.log("Error: YouTube");
  311. return false;
  312. }
  313. }
  314.  
  315. // Add Theme
  316. if (swmpConfig.theme != undefined) {
  317. this.container.classList.add(`swmp-theme-${swmpConfig.theme}`);
  318. }
  319.  
  320. if (this.type == 'video' || this.type == 'audio') {
  321. // Create buttons
  322. //this.prepareSharedEventsInit();
  323. this.prepareSharedEvents();
  324. this.prepareControls();
  325. this.preparePlayerEvents();
  326. this.prepareSettings();
  327. } else if (this.type == 'youtube') {
  328. //this.prepareSharedEvents();
  329. //this.prepareYoutubeEvents();
  330. }
  331.  
  332. this.prepareSharedEventsInit();
  333.  
  334. }
  335.  
  336. nextMedia() {
  337. if (this.playlistLocation === this.playlist.length -1) {
  338. return;
  339. }
  340.  
  341. this.playlistLocation++;
  342. this.url = this.playlist[this.playlistLocation];
  343. this.windowTitle.textContent = `[${this.playlistLocation+1}/${this.playlist.length}] ${this.url}`;
  344.  
  345. this.refreshPlayer();
  346. }
  347.  
  348. previousMedia() {
  349. if (this.playlistLocation === 0) {
  350. return;
  351. }
  352.  
  353. this.playlistLocation--;
  354. this.url = this.playlist[this.playlistLocation];
  355. this.windowTitle.textContent = `[${this.playlistLocation+1}/${this.playlist.length}] ${this.url}`;
  356.  
  357. this.refreshPlayer();
  358. }
  359.  
  360. clearPlayer() {
  361.  
  362. if (this.player.interval != undefined) {
  363. clearInterval(this.player.interval);
  364. }
  365.  
  366. if (this.closeError != undefined) {
  367. this.closeError.remove();
  368. }
  369. /*if (timeInterval != undefined) {
  370. clearInterval(timeInterval); //the yt timer is completely borken
  371. }*/
  372.  
  373. this.container.classList.remove('swmp-video');
  374. this.container.classList.remove('swmp-audio');
  375. this.container.classList.remove('swmp-youtube');
  376.  
  377. if (this.settingsContainer != undefined) {
  378. this.settingsContainer.innerHTML = null;
  379. this.settingsContainer.remove();
  380. }
  381. this.controls.innerHTML = null;
  382. this.controls.remove();
  383. this.playerContainer.innerHTML = null;
  384. this.playerContainer.remove();
  385. this.player.innerHTML = null;
  386. this.player.remove();
  387.  
  388.  
  389. }
  390.  
  391. refreshPlayer() {
  392. this.clearPlayer();
  393.  
  394. this.checkURL(this.url);
  395.  
  396. if (this.type == 'youtube') {
  397. this.prepareYoutube();
  398. }
  399.  
  400. if (this.type == 'video' || this.type == 'audio') {
  401. this.preparePlayer();
  402. this.prepareSharedEvents();
  403. this.prepareControls();
  404. this.preparePlayerEvents();
  405. this.prepareSettings();
  406. }
  407. }
  408.  
  409. prepareWindow() {
  410. this.windowContainer = document.createElement('div');
  411. this.windowContainer.setAttribute('class', 'swmp swmp-window swmp-window-container');
  412. this.container.appendChild(this.windowContainer);
  413.  
  414. // Create Title/WindowDragbar and put inside Window Container
  415. this.windowTitlebar = document.createElement('span');
  416. this.windowTitlebar.setAttribute('class', 'swmp swmp-window swmp-window-titlebar');
  417.  
  418. this.windowTitle = document.createElement('span');
  419. this.windowTitle.setAttribute('class', 'swmp swmp-window swmp-window-title');
  420.  
  421. if (this.playlist != undefined) {
  422. if (this.title != undefined) {
  423. this.windowTitle.textContent = `[${this.playlistLocation+1}/${this.playlist.length}] ${this.title}`;
  424. } else {
  425. this.windowTitle.textContent = `[${this.playlistLocation+1}/${this.playlist.length}] ${this.url}`;
  426. }
  427. } else {
  428. if (this.title != undefined) {
  429. this.windowTitle.textContent = this.title;
  430. } else {
  431. this.windowTitle.textContent = this.url;
  432. }
  433. }
  434.  
  435.  
  436.  
  437. this.windowTitlebar.appendChild(this.windowTitle);
  438. this.windowContainer.appendChild(this.windowTitlebar);
  439.  
  440. // Create Window Buttons Container
  441. this.windowButtonsContain = document.createElement('span');
  442. this.windowButtonsContain.setAttribute('class', 'swmp swmp-window swmp-window-buttons-contain');
  443. this.windowContainer.appendChild(this.windowButtonsContain);
  444.  
  445. // Create Minimize Button (Video only)
  446. if (this.type == 'video' || this.type == 'youtube') {
  447. this.windowMinimize = document.createElement('button');
  448. this.windowMinimize.setAttribute('class', 'swmp swmp-button swmp-window-minimize');
  449. this.windowMinimize.innerHTML = '<span></span>';
  450. this.windowMinimize.addEventListener('click', (event) => {
  451. event.preventDefault();
  452. if (this.container.classList.contains('swmp-minimized') ) {
  453. this.container.classList.remove('swmp-minimized');
  454. } else {
  455. this.container.classList.add('swmp-minimized');
  456. }
  457. });
  458. this.windowButtonsContain.appendChild(this.windowMinimize);
  459. }
  460.  
  461. // Create Maximize Button (Video only)
  462. if (this.type == 'video' || this.type == 'youtube') {
  463. this.windowMaximize = document.createElement('button');
  464. this.windowMaximize.setAttribute('class', 'swmp swmp-button swmp-window-maximize');
  465. this.windowMaximize.innerHTML = '<span></span>';
  466. this.windowMaximize.addEventListener('click', this.toggleMaximize);
  467. this.windowButtonsContain.appendChild(this.windowMaximize);
  468. }
  469.  
  470. // Create Close Button
  471. this.windowClose = document.createElement('button');
  472. this.windowClose.setAttribute('class', 'swmp swmp-button swmp-window-close');
  473. this.windowClose.innerHTML = '<span></span>';
  474. this.windowClose.addEventListener('click', (event) => {
  475. event.preventDefault();
  476. this.container.remove();
  477. });
  478. this.windowButtonsContain.appendChild(this.windowClose);
  479.  
  480. // Window Event
  481. if (swmpConfig.windowed != 'false') {
  482. this.makeDraggable(this.container);
  483. } else {
  484. this.windowTitlebar.style.cursor = 'default';
  485. }
  486.  
  487.  
  488. // Disable Default Context Menu on Titlebar
  489. this.windowTitlebar.addEventListener('contextmenu', function(evt) {
  490. evt.preventDefault();
  491. }, false);
  492. // Right Click Event
  493. this.windowTitlebar.addEventListener('mousedown', (event) => {
  494. event.preventDefault();
  495. switch (event.which) {
  496. case 3: //rightclick
  497. this.openSettings();
  498. break;
  499. }
  500. });
  501.  
  502. // Maximize
  503. this.windowTitlebar.addEventListener('dblclick', this.toggleMaximize);
  504.  
  505. }
  506.  
  507. prepareControls() {
  508. // Create and put Controls inside Container
  509. this.controls = document.createElement('div');
  510. this.controls.setAttribute('class', 'swmp swmp-controls');
  511. this.container.appendChild(this.controls);
  512.  
  513. // Disable Default Context Menu on Controls
  514. this.controls.addEventListener('contextmenu', function(evt) {
  515. evt.preventDefault();
  516. }, false);
  517. // Right Click Event
  518. this.controls.addEventListener('mousedown', (event) => {
  519. switch (event.which) {
  520. case 3: //rightclick
  521. this.openSettings();
  522. break;
  523. }
  524. });
  525.  
  526. // Create Progress/Seeker Container
  527. this.seekContain = document.createElement('span');
  528. this.seekContain.setAttribute('class', 'swmp swmp-seek-container');
  529. //this.seekContain.textContent = 'seek-cntnr';
  530. this.controls.appendChild(this.seekContain);
  531.  
  532. // Create Progress Bar
  533. this.progress = document.createElement('progress');
  534. this.progress.setAttribute('value', '0');
  535. this.progress.setAttribute('min', '0');
  536. this.progress.setAttribute('max', '1000');
  537. this.progress.setAttribute('step', '1');
  538. this.progress.setAttribute('class', 'swmp swmp-progress');
  539. this.seekContain.appendChild(this.progress);
  540.  
  541. // Create Seeker Input
  542. this.seeker = document.createElement('input');
  543. this.seeker.setAttribute('type', 'range');
  544. this.seeker.setAttribute('value', '0');
  545. this.seeker.setAttribute('min', '0');
  546. this.seeker.setAttribute('max', '1000');
  547. this.seeker.setAttribute('step', '1');
  548. this.seeker.setAttribute('class', 'swmp swmp-seeker');
  549. this.seekContain.appendChild(this.seeker);
  550.  
  551. // Create a Row Bottom Container
  552. this.bottomRow = document.createElement('span');
  553. this.bottomRow.setAttribute('class', 'swmp swmp-row-bottom');
  554. this.controls.appendChild(this.bottomRow);
  555.  
  556. // Create Buttons Container
  557. this.buttonsContain = document.createElement('span');
  558. this.buttonsContain.setAttribute('class', 'swmp swmp-buttons-container');
  559. this.bottomRow.appendChild(this.buttonsContain);
  560.  
  561. // Create Play/Pause Button and put inside Controls
  562. this.playbutton = document.createElement('button');
  563. this.playbutton.setAttribute('class', 'swmp swmp-button swmp-playbutton');
  564. this.playbutton.innerHTML = "<span></span>"; // ◀
  565. this.playbutton.addEventListener("click", event => {
  566. event.preventDefault();
  567.  
  568. if (this.type == 'video' || this.type == 'audio') {
  569. if (this.player.paused ) {
  570. this.player.play();
  571. } else {
  572. this.player.pause();
  573. }
  574. } else if (this.type == 'youtube') {
  575. this.togglePlay();
  576. }
  577. });
  578. this.buttonsContain.appendChild(this.playbutton);
  579.  
  580. // Create a Stop/Reload Button and put inside Controls
  581. this.stopbutton = document.createElement('button');
  582. this.stopbutton.setAttribute('class', 'swmp swmp-button swmp-stopbutton');
  583. this.stopbutton.innerHTML = "<span></span>"; // ■
  584. this.stopbutton.addEventListener("click", event => {
  585. event.preventDefault();
  586. if (this.loaded != true) {
  587. console.log('Userscript: Can\'t send stop yet.');
  588. return;
  589. }
  590. if (this.type == 'video' || this.type == 'audio') {
  591. this.player.pause();
  592. this.seeker.value = 0;
  593. this.seeker.setAttribute("value", 0);
  594. this.progress.value = 0;
  595. this.progress.setAttribute("value", 0);
  596. this.player.currentTime = 0;
  597. this.currentTimer.textContent = '00:00';
  598. } else if (this.type == 'youtube') {
  599. this.seeker.value = 0;
  600. this.seeker.setAttribute("value", 0);
  601. this.progress.value = 0;
  602. this.progress.setAttribute("value", 0);
  603. this.ytplayer.stopVideo();
  604. this.currentTimer.textContent = '00:00';
  605. }
  606. });
  607. this.buttonsContain.appendChild(this.stopbutton);
  608.  
  609. if (this.playlist) {
  610. this.previousButton = document.createElement('button');
  611. this.previousButton.setAttribute('class', 'swmp swmp-button swmp-playlist swmp-playlist-prev');
  612. this.previousButton.innerHTML = "<span></span>";
  613. this.previousButton.addEventListener('click', event => {
  614. event.preventDefault();
  615. this.previousMedia();
  616. });
  617. this.buttonsContain.appendChild(this.previousButton);
  618.  
  619. this.nextButton = document.createElement('button');
  620. this.nextButton.setAttribute('class', 'swmp swmp-button swmp-playlist');
  621. this.nextButton.innerHTML = "<span></span>";
  622. this.nextButton.addEventListener('click', event => {
  623. event.preventDefault();
  624. this.nextMedia();
  625. });
  626. this.buttonsContain.appendChild(this.nextButton);
  627. }
  628.  
  629. // Create a Volume Container
  630. this.volumeContain = document.createElement('span');
  631. this.volumeContain.setAttribute('class', 'swmp swmp-volume-container');
  632. this.bottomRow.appendChild(this.volumeContain);
  633.  
  634. // Create a Volume Button and put inside Buttons Container
  635. this.volumeButton = document.createElement('button');
  636. this.volumeButton.setAttribute('class', 'swmp swmp-button swmp-volume');
  637. this.volumeButton.innerHTML = "<span></span>";
  638. this.volumeButton.addEventListener("click", event => {
  639. event.preventDefault();
  640. this.toggleMute();
  641. });
  642. this.buttonsContain.appendChild(this.volumeButton);
  643.  
  644. // Create a Volume Progress and put inside Volume Container
  645. this.volumeProgress = document.createElement('progress');
  646. this.volumeProgress.setAttribute('value', this.defaultVolume);
  647. this.volumeProgress.setAttribute('min', '0');
  648. this.volumeProgress.setAttribute('max', '100');
  649. this.volumeProgress.setAttribute('step', '1');
  650. this.volumeProgress.setAttribute('class', 'swmp swmp-volume swmp-progress');
  651. this.volumeContain.appendChild(this.volumeProgress);
  652.  
  653. // Create a Volume Input and put inside Volume Container
  654. this.volumeRange = document.createElement('input');
  655. this.volumeRange.setAttribute('type', 'range');
  656. this.volumeRange.setAttribute('value', this.defaultVolume);
  657. this.volumeRange.setAttribute('min', '0');
  658. this.volumeRange.setAttribute('max', '100');
  659. this.volumeRange.setAttribute('step', '1');
  660. this.volumeRange.setAttribute('class', 'swmp swmp-volume swmp-range');
  661. this.volumeContain.appendChild(this.volumeRange);
  662.  
  663. // Create a Timer Container and put inside Controls
  664. this.timerContain = document.createElement('span');
  665. this.timerContain.setAttribute('class', 'swmp swmp-timer-container');
  666. this.bottomRow.appendChild(this.timerContain);
  667.  
  668. // Create a timerCurrent and put inside Timer Container
  669. this.currentTimer = document.createElement('span');
  670. this.currentTimer.setAttribute('class', 'swmp swmp-time swmp-current');
  671. this.currentTimer.textContent = '00:00';
  672. this.timerContain.appendChild(this.currentTimer);
  673.  
  674. // Add a separator between timer
  675. this.timerSeperator = document.createElement('span');
  676. this.timerSeperator.setAttribute('class', 'swmp swmp-time swmp-separator');
  677. this.timerSeperator.textContent = '/';
  678. this.timerContain.appendChild(this.timerSeperator);
  679.  
  680. // Create a totalTimer and put inside Timer Container
  681. this.totalTimer = document.createElement('span');
  682. this.totalTimer.setAttribute('class', 'swmp swmp-time swmp-total');
  683. this.totalTimer.textContent = '00:00';
  684. this.timerContain.appendChild(this.totalTimer);
  685.  
  686. // Create a Fullscreen Button and put inside Bottom Row Container
  687. if (this.type == 'video' || this.type == 'youtube' || (this.type == 'audio' && this.playlist != null) ) {
  688. this.fullscreenbutton = document.createElement('button');
  689. this.fullscreenbutton.setAttribute('class', 'swmp swmp-button swmp-fullscreen');
  690. this.fullscreenbutton.innerHTML = "<span></span>"; // ▣
  691. this.fullscreenbutton.addEventListener("click", event => {
  692. event.preventDefault();
  693. this.fullscreen(this.container);
  694. });
  695. this.bottomRow.appendChild(this.fullscreenbutton);
  696. }
  697. }
  698.  
  699. prepareSettings() {
  700.  
  701. this.openSettings = () => {
  702. // Settings Menu
  703.  
  704. if (this.container.querySelector('.swmp-settings-container') != null) {
  705. this.settingsContainer.remove();
  706. return false; //Already open
  707. }
  708.  
  709. this.settingsContainer = document.createElement('div');
  710. this.settingsContainer.setAttribute('class', 'swmp swmp-settings swmp-settings-container');
  711. this.settingsContainer.innerHTML = 'Settings:';
  712.  
  713.  
  714. this.br = document.createElement('br');
  715. this.settingsContainer.appendChild(this.br);
  716.  
  717. this.themeSelector = document.createElement('select');
  718. this.themeSelector.setAttribute('class', 'swmp swmp-selector swmp-settings swmp-theme-selector');
  719.  
  720. this.themes = '';
  721. swmpConfig.themes.forEach(theme => {
  722. if (localStorage.swmpTheme == theme[0]) {
  723. this.themeSelector.value = theme[0];
  724. this.themes += `<option value="${theme[0]}" selected>${theme[1]}</option>`;
  725. } else {
  726. this.themes += `<option value="${theme[0]}">${theme[1]}</option>`;
  727. }
  728. });
  729. this.themeSelector.innerHTML = this.themes;
  730.  
  731. this.themeSelector.onchange = (event) => {
  732. if (this.themeSelector.value == '') {
  733. return false;
  734. } else {
  735. this.removeClassByPrefix(this.container, 'swmp-theme-'); //regex remove [theme-*]
  736. this.container.classList.add(`swmp-theme-${this.themeSelector.value}`);
  737. swmpConfig.theme = this.themeSelector.value;
  738. localStorage.swmpTheme = this.themeSelector.value;
  739. }
  740. }
  741.  
  742. this.settingsContainer.appendChild(this.themeSelector);
  743.  
  744. // Autoplay Settings
  745. this.autoplayLabel = document.createElement('label');
  746. this.autoplayLabel.setAttribute('class', 'swmp swmp-settings swmp-label swmp-autoplay-label');
  747. this.autoplayLabel.textContent = 'Autoplay';
  748. this.autoplayCheck = document.createElement('input');
  749. this.autoplayCheck.setAttribute('class', 'swmp swmp-settings swmp-input swmp-autoplay-input');
  750. this.autoplayCheck.setAttribute('type', 'checkbox');
  751. if (localStorage.swmpAutoplay == 'true') {
  752. this.autoplayCheck.setAttribute('checked', 'checked');
  753. }
  754. this.autoplayCheck.addEventListener('change', (event) => {
  755. if (this.autoplayCheck.checked == true) {
  756. this.autoplayCheck.setAttribute('checked', 'checked');
  757. localStorage.swmpAutoplay = 'true';
  758. swmpConfig.autoplay = 'true';
  759. } else {
  760. this.autoplayCheck.removeAttribute('checked');
  761. localStorage.swmpAutoplay = 'false';
  762. swmpConfig.autoplay = 'false';
  763. }
  764. });
  765. this.autoplayLabel.appendChild(this.autoplayCheck);
  766. this.settingsContainer.appendChild(this.autoplayLabel);
  767.  
  768. // Loop Settings
  769. this.loopLabel = document.createElement('label');
  770. this.loopLabel.setAttribute('class', 'swmp swmp-settings swmp-label swmp-loop-label');
  771. this.loopLabel.textContent = 'Loop';
  772. this.loopCheck = document.createElement('input');
  773. this.loopCheck.setAttribute('class', 'swmp swmp-settings swmp-input swmp-loop-input');
  774. this.loopCheck.setAttribute('type', 'checkbox');
  775. if (localStorage.swmpLoop == 'true') {
  776. this.loopCheck.setAttribute('checked', 'checked');
  777. }
  778. this.loopCheck.addEventListener('change', (event) => {
  779. if (this.loopCheck.checked == true) {
  780. this.loopCheck.setAttribute('checked', 'checked');
  781. if (this.type == 'video' || this.type == 'audio') {
  782. this.player.setAttribute('loop', 'true');
  783. } else if (this.type == 'youtube') {
  784. // Youtube Loop
  785. }
  786. localStorage.swmpLoop = 'true';
  787. swmpConfig.loop = 'true';
  788. } else {
  789. this.loopCheck.removeAttribute('checked');
  790. if (this.type == 'video' || this.type == 'audio') {
  791. this.player.removeAttribute('loop');
  792. } else if (this.type == 'youtube') {
  793. // Youtube Loop
  794. }
  795. localStorage.swmpLoop = 'false';
  796. swmpConfig.loop = 'false';
  797. }
  798. });
  799. this.loopLabel.appendChild(this.loopCheck);
  800. this.settingsContainer.appendChild(this.loopLabel);
  801. // Multiple Players Settings
  802. this.multiLabel = document.createElement('label');
  803. this.multiLabel.setAttribute('class', 'swmp swmp-settings swmp-label swmp-multi-label');
  804. this.multiLabel.textContent = 'Multi';
  805. this.multiCheck = document.createElement('input');
  806. this.multiCheck.setAttribute('class', 'swmp swmp-settings swmp-input swmp-multi-input');
  807. this.multiCheck.setAttribute('type', 'checkbox');
  808. if (localStorage.swmpAllowMultiple == 'true') {
  809. this.multiCheck.setAttribute('checked', 'checked');
  810. }
  811. this.multiCheck.addEventListener('change', (event) => {
  812. if (this.multiCheck.checked == true) {
  813. this.multiCheck.setAttribute('checked', 'checked');
  814. localStorage.swmpAllowMultiple = 'true';
  815. swmpConfig.allowMultiple = 'true';
  816. } else {
  817. this.multiCheck.removeAttribute('checked');
  818. localStorage.swmpAllowMultiple = 'false';
  819. swmpConfig.allowMultiple = 'false';
  820. }
  821. });
  822. this.multiLabel.appendChild(this.multiCheck);
  823. this.settingsContainer.appendChild(this.multiLabel);
  824. this.br = document.createElement('br');
  825. this.settingsContainer.appendChild(this.br);
  826. // Volume Scroll Settings
  827. this.volumeScrollLabel = document.createElement('label');
  828. this.volumeScrollLabel.setAttribute('class', 'swmp swmp-settings swmp-label swmp-volumescroll-label');
  829. this.volumeScrollLabel.textContent = 'Volume Scroll';
  830. this.volumeScrollCheck = document.createElement('input');
  831. this.volumeScrollCheck.setAttribute('class', 'swmp swmp-settings swmp-input swmp-volumescroll-input');
  832. this.volumeScrollCheck.setAttribute('type', 'checkbox');
  833. if (localStorage.swmpVolumeScroll == 'true') {
  834. this.volumeScrollCheck.setAttribute('checked', 'checked');
  835. }
  836. this.volumeScrollCheck.addEventListener('change', (event) => {
  837. if (this.volumeScrollCheck.checked == true) {
  838. this.volumeScrollCheck.setAttribute('checked', 'checked');
  839. localStorage.swmpVolumeScroll = 'true';
  840. swmpConfig.volumeScroll = 'true';
  841. } else {
  842. this.volumeScrollCheck.removeAttribute('checked');
  843. localStorage.swmpVolumeScroll = 'false';
  844. swmpConfig.volumeScroll = 'false';
  845. }
  846. });
  847. this.volumeScrollLabel.appendChild(this.volumeScrollCheck);
  848. this.settingsContainer.appendChild(this.volumeScrollLabel);
  849.  
  850. // Override Download link Attribute Settings
  851. this.downloadAttributeLabel = document.createElement('label');
  852. this.downloadAttributeLabel.setAttribute('class', 'swmp swmp-settings swmp-label swmp-downloadattribute-label');
  853. this.downloadAttributeLabel.textContent = 'Override Download';
  854. this.downloadAttributeCheck = document.createElement('input');
  855. this.downloadAttributeCheck.setAttribute('class', 'swmp swmp-settings swmp-input swmp-downloadattribute-input');
  856. this.downloadAttributeCheck.setAttribute('type', 'checkbox');
  857. if (localStorage.swmpDownloadAttribute == 'true') {
  858. this.downloadAttributeCheck.setAttribute('checked', 'checked');
  859. }
  860. this.downloadAttributeCheck.addEventListener('change', (event) => {
  861. if (this.downloadAttributeCheck.checked == true) {
  862. this.downloadAttributeCheck.setAttribute('checked', 'checked');
  863. localStorage.swmpDownloadAttribute = 'true';
  864. swmpConfig.downloadAttribute = 'true';
  865. } else {
  866. this.downloadAttributeCheck.removeAttribute('checked');
  867. localStorage.swmpDownloadAttribute = 'false';
  868. swmpConfig.downloadAttribute = 'false';
  869. }
  870. });
  871. this.downloadAttributeLabel.appendChild(this.downloadAttributeCheck);
  872. this.settingsContainer.appendChild(this.downloadAttributeLabel);
  873.  
  874.  
  875. // Replace doubleclick fullscreen with doubleclick maximize
  876. this.doubleclickMaximizeLabel = document.createElement('label');
  877. this.doubleclickMaximizeLabel.setAttribute('class', 'swmp swmp-settings swmp-label swmp-doubleclickmaximize-label');
  878. this.doubleclickMaximizeLabel.textContent = 'Doubleclick Maximize';
  879. this.doubleclickMaximizeCheck = document.createElement('input');
  880. this.doubleclickMaximizeCheck.setAttribute('class', 'swmp swmp-settings swmp-label swmp-doubleclickmaximize-input');
  881. this.doubleclickMaximizeCheck.setAttribute('type', 'checkbox');
  882. if (localStorage.swmpDoubleclickMaximize == 'true') {
  883. this.doubleclickMaximizeCheck.setAttribute('checked', 'checked');
  884. }
  885. this.doubleclickMaximizeCheck.addEventListener('change', (event) => {
  886. if (this.doubleclickMaximizeCheck.checked == true) {
  887. this.doubleclickMaximizeCheck.setAttribute('checked', 'checked');
  888. localStorage.swmpDoubleclickMaximize = 'true';
  889. swmpConfig.doubleclickMaximize = 'true';
  890. } else {
  891. this.doubleclickMaximizeCheck.removeAttribute('checked');
  892. localStorage.swmpDoubleclickMaximize = 'false';
  893. swmpConfig.doubleclickMaximize = 'false';
  894. }
  895. });
  896. this.doubleclickMaximizeLabel.appendChild(this.doubleclickMaximizeCheck);
  897. this.settingsContainer.appendChild(this.doubleclickMaximizeLabel);
  898.  
  899.  
  900.  
  901. this.container.appendChild(this.settingsContainer);
  902.  
  903. }
  904. }
  905.  
  906. preparePlayer() {
  907. // Create Player HTML5 Video or Audio format.
  908. if (this.type == 'video') {
  909. this.container.classList.add('swmp-video');
  910. this.player = document.createElement('video');
  911. this.player.setAttribute('class', 'swmp swmp-video swmp-player');
  912. if (this.poster != false && this.poster != undefined) {
  913. this.player.setAttribute('poster', this.poster);
  914. }
  915. } else if (this.type == 'audio') {
  916. this.container.classList.add('swmp-audio');
  917. this.player = document.createElement('audio');
  918. this.player.setAttribute('class', 'swmp swmp-audio swmp-player');
  919. } else {
  920. console.log(`SWMP Error: invalid type of ${this.type}.`);
  921. return false;
  922. }
  923.  
  924. // Check Format Support
  925. if (this.mime != undefined) {
  926. if (this.player.canPlayType(this.mime) == '') {
  927. this.closeError = document.createElement('span');
  928. this.closeError.innerHTML = `Your browser can't play this format: ${this.mime}`;
  929. /*this.container.addEventListener('click', (event) => {
  930. this.container.remove();
  931. });*/
  932. this.container.appendChild(this.closeError);
  933. //return false;
  934. }
  935. }
  936.  
  937. // Preload metadata
  938. this.player.setAttribute('preload', 'metadata');
  939.  
  940. // Create Player Container and Put inside Container
  941. this.playerContainer = document.createElement('div');
  942. this.playerContainer.setAttribute('class', 'swmp swmp-player-container');
  943. this.container.appendChild(this.playerContainer);
  944. // Put Player inside Video Container
  945. this.playerContainer.appendChild(this.player);
  946.  
  947. // Create and put Source inside Player
  948. this.source = document.createElement('source');
  949. this.source.setAttribute('src', this.url);
  950. if (this.mime != undefined) {
  951. this.source.setAttribute('type', this.mime);
  952. }
  953. this.player.appendChild(this.source);
  954.  
  955. // Is Autoplay?
  956. if ( (swmpConfig.autoplay == true || swmpConfig.autoplay == 'true') && this.autoplay != false) {
  957. this.player.setAttribute('autoplay', true);
  958. }
  959.  
  960. // Is Loop?
  961. if ( (swmpConfig.loop == true || swmpConfig.loop == 'true') && this.loop != false) {
  962. this.player.setAttribute('loop', true);
  963. }
  964. }
  965.  
  966. prepareYoutube() {
  967.  
  968. console.log('SWMP: prepareYoutube() ');
  969.  
  970. if (youtubeIsLoaded == false) {
  971. // Include YouTube iframe API if not already added:
  972. if (!document.getElementById('swmp-youtube-api')) {
  973. console.log("SWMP: Loading YouTube API");
  974. var tag = document.createElement('script');
  975. tag.src = "https://www.youtube.com/iframe_api";
  976. tag.setAttribute('id', 'swmp-youtube-api');
  977. var firstScriptTag = document.getElementsByTagName('script')[0];
  978. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  979. }
  980. gmwindow.onYouTubeIframeAPIReady = (event) => {
  981. youtubeIsLoaded = true;
  982. console.log('SWMP: Firing YouTube Ready Event from inside PrepareYouTube()');
  983. this.makeYoutube();
  984. }
  985. } else {
  986. // API Already loaded, just make video.
  987. console.log('SWMP: YT API Already loaded, make vid');
  988. this.makeYoutube();
  989. }
  990. }
  991.  
  992. makeYoutube() {
  993. // Make new player
  994. console.log('SWMP: makeYoutube()');
  995. this.playerfirstrun = true;
  996. var self = this;
  997. this.loaded = false;
  998. this.container.classList.add('swmp-youtube');
  999. this.onPlayerReady = (event) => {
  1000. this.loaded = true;
  1001. if (!this.container.classList.contains('swmp-youtube')) { //cancel if already changed playlist
  1002. return;
  1003. }
  1004. console.log('SWMP Youtube: onPlayerReady()');
  1005. if (this.playerfirstrun == true) {
  1006. this.playerfirstrun = false;
  1007. this.playerContainer.appendChild(this.ytplayer.getIframe() );
  1008. if (this.playlist) {
  1009. this.windowTitle.textContent = `[${this.playlistLocation+1}/${this.playlist.length}] ${this.ytplayer.getVideoData().title}`;
  1010. } else {
  1011. this.windowTitle.textContent = this.ytplayer.getVideoData().title;
  1012. }
  1013. this.totalTimer.textContent = this.formatSeconds(this.ytplayer.getDuration() );
  1014. this.ytplayer.getIframe().style.display = 'inherit';
  1015.  
  1016. this.ytplayer.setVolume(this.defaultVolume);
  1017. this._volume = this.defaultVolume;
  1018. /*if (swmpConfig.muted == 'true') {
  1019. this.ytplayer.mute();
  1020. this.volumeButton.classList.add('swmp-mute');
  1021. } else {
  1022. this.ytplayer.unMute();
  1023. }*/
  1024. this.ytplayer.unMute();
  1025.  
  1026. if (this.autoplay == 'true') {
  1027. this.ytplayer.playVideo();
  1028. this.playbutton.classList.add('swmp-playing');
  1029. }
  1030.  
  1031.  
  1032. }
  1033.  
  1034. window.addEventListener('message', function(event) {
  1035. if (event.source === self.ytplayer.getIframe().contentWindow) {
  1036. var data = JSON.parse(event.data);
  1037. //console.log(data);
  1038.  
  1039. // Dispatch Volume and Mute Event -- These are sent together by iframe.
  1040. if (data.event === "infoDelivery" && data.info && data.info.volume) {
  1041. //console.log(data.info.volume);
  1042. //console.log(data.info.muted);
  1043. if (data.info.volume != self._volume) {
  1044. //console.log(data.info.volume);
  1045. self.updateVolume();
  1046. }
  1047. }
  1048.  
  1049. // Dispatch on Time Event
  1050. if (data.event === "infoDelivery" && data.info && data.info.currentTime) {
  1051. //console.log(data.info.currentTime);
  1052. self.currentTimer.textContent = self.formatSeconds(data.info.currentTime);
  1053. }
  1054.  
  1055. // Dispatch on Player state event
  1056. if (data.event === "infoDelivery" && data.info && data.info.playerState) {
  1057. //console.log(data.info.playerState);
  1058. self.updatePlay();
  1059. self.updateVolume();
  1060. }
  1061. }
  1062. });
  1063.  
  1064. }
  1065.  
  1066. this.videogenerate = document.createElement('div');
  1067. this.videogenerate.style.display = 'none';
  1068. this.videogenerate.setAttribute('id', `ytplayer-${this.id}`);
  1069. document.body.appendChild(this.videogenerate);
  1070. this.youtubeautoplay = (swmpConfig.autoplay=='true')?1:0;
  1071. this.youtubeloop = (swmpConfig.loop=='true')?1:0;
  1072.  
  1073. this.ytplayer = new gmwindow.YT.Player(`ytplayer-${this.id}`, {
  1074. height: '1080',
  1075. width: '1920', // Allows quality to increase to 1080p. Can't be forced by API, need fast internet.
  1076. videoId: this.videoid,
  1077. playerVars: {
  1078. 'playsinline': 1,
  1079. 'autoplay': this.youtubeautoplay,
  1080. 'loop': this.youtubeloop,
  1081. //'origin': window.location.href,
  1082. 'rel': 0,
  1083. 'modestbranding': 1,
  1084. 'controls': 0
  1085. },
  1086. events: {
  1087. 'onReady': this.onPlayerReady
  1088. }
  1089. });
  1090. //console.log(this.ytplayer);
  1091.  
  1092. this.playerContainer = document.createElement('div');
  1093. this.playerContainer.setAttribute('class', 'swmp-player-container');
  1094.  
  1095. this.container.appendChild(this.playerContainer);
  1096.  
  1097. this.prepareSettings();
  1098. this.prepareControls();
  1099. this.prepareSharedEvents();
  1100. this.prepareYoutubeEvents();
  1101.  
  1102. if (!this.container.classList.contains('swmp-youtube')) { //cancel if already changed playlist
  1103. this.videogenerate.remove();
  1104. this.ytplayer.remove();
  1105. return false;
  1106. }
  1107.  
  1108. }
  1109.  
  1110.  
  1111. prepareYoutubeEvents() {
  1112.  
  1113. this.togglePlay = function() {
  1114. if (this.loaded != true) {
  1115. console.log('Userscript: YT must load before togglePlay.');
  1116. return;
  1117. }
  1118. if (this.ytplayer.getPlayerState() == 1) { // Playing
  1119. this.ytplayer.pauseVideo();
  1120. } else { // -1 unstarted, 0 ended, 1 playing, 2 Pause, 3 buffering, 5 video cued
  1121. this.ytplayer.playVideo();
  1122. this.updateTime();
  1123. }
  1124. }
  1125.  
  1126. this.seekBack = function() {
  1127. var time = this.ytplayer.getCurrentTime();
  1128. var max = this.ytplayer.getDuration();
  1129. if (time < 5) {
  1130. time = 0;
  1131. } else {
  1132. time = time - 5;
  1133. }
  1134. this.ytplayer.seekTo(time);
  1135.  
  1136. this.seeker.value = Math.floor(time / max * this.seeker.max);
  1137. this.seeker.setAttribute('value', this.seeker.value);
  1138. this.progress.value = this.seeker.value;
  1139. this.progress.setAttribute('value', this.seeker.value);
  1140. }
  1141.  
  1142. this.seekForward = function() {
  1143. var time = this.ytplayer.getCurrentTime();
  1144. var max = this.ytplayer.getDuration();
  1145. if (time+5 >= max ) {
  1146. time = max;
  1147. } else {
  1148. time = time + 5;
  1149. }
  1150. this.ytplayer.seekTo(time);
  1151.  
  1152. this.seeker.value = Math.floor(time / max * this.seeker.max);
  1153. this.seeker.setAttribute('value', this.seeker.value);
  1154. this.progress.value = this.seeker.value;
  1155. this.progress.setAttribute('value', this.seeker.value);
  1156.  
  1157. }
  1158.  
  1159. var timeInterval;
  1160. this.updatePlay = () => {
  1161.  
  1162. this._playerState = this.ytplayer.getPlayerState();
  1163.  
  1164. if (this._playerState == 1) { // Playing
  1165. this.playbutton.classList.add('swmp-playing');
  1166. this.updateTime();
  1167.  
  1168.  
  1169. var self = this;
  1170. var timeInterval = window.setInterval((event) => {
  1171. self._playerState = self.ytplayer.getPlayerState();
  1172. if (self._playerState == 1) {
  1173. self.updateTime();
  1174. }
  1175. if (self._playerState == 0) { //Ended
  1176. self.updatePlay();
  1177. }
  1178. //console.log('time'); //still bugged on close unlike the other interval, does close on playlist change
  1179. if (self.type != 'youtube' || self._playerState != 1 || self.container == null || self.container == undefined || self.container == false) {
  1180. clearInterval(timeInterval);
  1181. timeInterval = null;
  1182. }
  1183. }, 40);
  1184.  
  1185. } else if (this._playerState == 0 || this._playerState == -1 || this._playerState == 2 ) { // -1 unstarted, 0 ended, 1 playing, 2 Pause, 3 buffering, 5 video cued
  1186. this.playbutton.classList.remove('swmp-playing');
  1187. }
  1188.  
  1189. if (this._playerState == 0 || this._playerState == -1 ) {
  1190. this.progress.value = 0;
  1191. this.progress.setAttribute('value', 0);
  1192. this.seeker.value = 0;
  1193. this.seeker.setAttribute('value', 0);
  1194. }
  1195.  
  1196. if (this._playerState == 0) {
  1197.  
  1198. if (this.playlist != undefined && swmpConfig.loop == 'false' && swmpConfig.autoplay == 'true') {
  1199. this.nextMedia();
  1200. return;
  1201. }
  1202.  
  1203. if (swmpConfig.loop == 'true') {
  1204. console.log('SWMP: uwah video loop');
  1205. this.ytplayer.seekTo(0);
  1206. this.ytplayer.playVideo();
  1207. }
  1208. }
  1209.  
  1210. }
  1211.  
  1212. this.seeker.oninput = (event) => {
  1213. if (this.loaded != true) {
  1214. console.log('Userscript: Video must load before seeking');
  1215. return;
  1216. }
  1217. //on mousedown temporary add a mute to avoid annoying seeking sounds?
  1218. this.ytplayer.seekTo( (this.ytplayer.getDuration() * this.seeker.value / this.seeker.max) );
  1219. this.progress.value = this.seeker.value;
  1220. this.progress.setAttribute('value', this.seeker.value);
  1221. this.updatePlay();
  1222. }
  1223.  
  1224. this.updateTime = () => {
  1225. this.seeker.value = Math.floor(this.ytplayer.getCurrentTime() / this.ytplayer.getDuration() * this.seeker.max);
  1226. this.seeker.setAttribute('value', this.seeker.value);
  1227. this.progress.value = this.seeker.value;
  1228. this.progress.setAttribute('value', this.seeker.value);
  1229. }
  1230.  
  1231.  
  1232. this.toggleMute = () => {
  1233.  
  1234. try {
  1235. if (this.ytplayer.isMuted() == true ) {
  1236. this.volumeButton.classList.remove('swmp-mute');
  1237. this._volume = this.ytplayer.getVolume() ;
  1238. this.ytplayer.unMute();
  1239. this.ytplayer.setVolume(this._volume);
  1240. this.volumeRange.setAttribute('value', this._volume );
  1241. this.volumeRange.value = this._volume;
  1242. this.volumeProgress.setAttribute('value', this._volume );
  1243. this.volumeProgress.value = this._volume;
  1244. swmpConfig.muted = 'false';
  1245. localStorage.swmpMuted = 'false';
  1246. } else {
  1247. this.volumeButton.classList.add('swmp-mute');
  1248. this.ytplayer.mute();
  1249. this.volumeRange.setAttribute('value', 0 );
  1250. this.volumeRange.value = 0;
  1251. this.volumeProgress.setAttribute('value', 0 );
  1252. this.volumeProgress.value = 0;
  1253. swmpConfig.muted = 'true';
  1254. localStorage.swmpMuted = 'true';
  1255. }
  1256. } catch(e) {
  1257. console.log('Userscript: '+e);
  1258. }
  1259. }
  1260.  
  1261. this.updateVolume = (firstrun = false) => {
  1262. if (this.loaded != true) {
  1263. return;
  1264. }
  1265. this.volumeButton.classList.remove('swmp-mute');
  1266.  
  1267. if (firstrun == true) {
  1268. this.volumeRange.setAttribute('value', this.defaultVolume);
  1269. this.volumeProgress.setAttribute('value', this.defaultVolume);
  1270. this._volume = this.defaultVolume;
  1271. this.volumeButton.classList.add('swmp-min');
  1272. }
  1273.  
  1274. this._volume = this.ytplayer.getVolume();
  1275.  
  1276. this.volumeRange.setAttribute('value', this.volumeRange.value );
  1277. this.volumeProgress.setAttribute('value', this.volumeRange.value );
  1278. this.ytplayer.setVolume(this.volumeRange.value);
  1279.  
  1280. if (this._volume > 50) {
  1281. this.volumeButton.classList.add('swmp-max');
  1282. this.volumeButton.classList.remove('swmp-med');
  1283. this.volumeButton.classList.remove('swmp-min');
  1284. } else if (this._volume > 10) {
  1285. this.volumeButton.classList.add('swmp-med');
  1286. this.volumeButton.classList.remove('swmp-max');
  1287. this.volumeButton.classList.remove('swmp-min');
  1288. } else if (this._volume > 5) {
  1289. this.volumeButton.classList.add('swmp-min');
  1290. this.volumeButton.classList.remove('swmp-max');
  1291. this.volumeButton.classList.remove('swmp-med');
  1292. }
  1293.  
  1294. localStorage.swmpVolume = this.volumeRange.value;
  1295. swmpConfig.volume = this.volumeRange.value;
  1296. }
  1297.  
  1298. this.volumeRange.oninput = (event) => {
  1299. this.updateVolume();
  1300. }
  1301.  
  1302. }
  1303.  
  1304. prepareSharedEventsInit() {
  1305.  
  1306. this.container.addEventListener('mousemove', event => {
  1307. this.container.classList.add('swmp-movingmouse');
  1308. clearTimeout(this.mousemovetimeout);
  1309. this.mousemovetimeout = window.setTimeout((event) => {
  1310. this.container.classList.remove('swmp-movingmouse');
  1311. }, 1500);
  1312. });
  1313.  
  1314. this.volumeScroll = (event) => {
  1315. if (swmpConfig.volumeScroll != 'true') {
  1316. return false;
  1317. }
  1318. event.preventDefault();
  1319. if (this.type == 'video' || this.type == 'audio') {
  1320. if (event.wheelDelta > 0) {
  1321. var vol = this.player.volume + 0.04;
  1322. if (vol > 1.0) {
  1323. vol = 1.0;
  1324. }
  1325. } else {
  1326. var vol = this.player.volume - 0.04;
  1327. if (vol < 0.0) {
  1328. vol = 0.0;
  1329. }
  1330. }
  1331. var newvolume = Math.floor(vol * 100);
  1332. } else if (this.type == 'youtube') {
  1333. if (event.wheelDelta > 0) {
  1334. var vol = this.ytplayer.getVolume() + 4;
  1335. if (vol > 100) {
  1336. vol = 100;
  1337. }
  1338. } else {
  1339. var vol = this.ytplayer.getVolume() - 4;
  1340. if (vol < 0) {
  1341. vol = 0;
  1342. }
  1343. }
  1344. var newvolume = vol;
  1345. }
  1346. this.volumeProgress.setAttribute('value', newvolume);
  1347. this.volumeProgress.value = newvolume;
  1348. this.volumeRange.setAttribute('value', newvolume);
  1349. this.volumeRange.value = newvolume;
  1350. this.updateVolume(); // Change icon blah blah
  1351. }
  1352.  
  1353. this.container.addEventListener('wheel', event => {
  1354. this.volumeScroll(event);
  1355. });
  1356.  
  1357. this.container.addEventListener('keydown', event => {
  1358. if (event.repeat) { return; } // Don't spam
  1359.  
  1360. switch(event.key) {
  1361. case ' ': //Space
  1362. this.togglePlay();
  1363. break;
  1364. case 'f':
  1365. this.fullscreen(this.container);
  1366. break;
  1367. case 'm':
  1368. this.toggleMute();
  1369. break;
  1370. case 'x':
  1371. this.container.remove();
  1372. break;
  1373. /*case 'q': // still need do youtube fix for fast skipping, otherwise would work well
  1374. if (this.playlist != undefined) { this.previousMedia(); } else { return; }
  1375. break;
  1376. case 'e':
  1377. if (this.playlist != undefined) { this.nextMedia(); } else { return; }
  1378. break;*/
  1379. case 'ArrowLeft': //left
  1380. this.seekBack();
  1381. break;
  1382. case 'ArrowRight': //right
  1383. this.seekForward();
  1384. break;
  1385. case 'Escape':
  1386. this.container.classList.remove('swmp-fullscreen');
  1387. this.container.classList.remove('swmp-maximized');
  1388. break;
  1389. default:
  1390. return;
  1391. }
  1392.  
  1393. event.preventDefault();
  1394.  
  1395. }, true);
  1396.  
  1397. this.container.addEventListener('fullscreenchange', event => {
  1398. const fullscreenElement =
  1399. document.fullscreenElement ||
  1400. document.mozFullScreenElement ||
  1401. document.webkitFullscreenElement ||
  1402. document.msFullscreenElement;
  1403.  
  1404. if (!fullscreenElement) {
  1405. this.container.classList.remove('swmp-fullscreen');
  1406. this.container.classList.remove('swmp-maximized');
  1407. } else {
  1408. this.container.classList.add('swmp-fullscreen');
  1409. }
  1410. });
  1411.  
  1412. this.fullscreen = (element) => {
  1413.  
  1414. const fullscreenElement =
  1415. document.fullscreenElement ||
  1416. document.mozFullScreenElement ||
  1417. document.webkitFullscreenElement ||
  1418. document.msFullscreenElement;
  1419.  
  1420. if (fullscreenElement) {
  1421. if (document.exitFullscreen) {
  1422. document.exitFullscreen();
  1423. } else if (document.mozCancelFullScreen) {
  1424. document.mozCancelFullScreen();
  1425. } else if (document.webkitExitFullscreen) {
  1426. window.scrollTo({
  1427. top: this._scroll['y'],
  1428. left: this._scroll['x'],
  1429. behavior: 'auto'
  1430. });
  1431. document.webkitExitFullscreen(); //Safari sucks
  1432. } else if (document.msExitFullscreen) {
  1433. document.msExitFullscreen();
  1434. }
  1435. this.container.classList.remove('swmp-fullscreen');
  1436. this.container.classList.remove('swmp-maximized');
  1437. } else {
  1438. if (element.requestFullscreen) {
  1439. element.requestFullscreen();
  1440. } else if (element.mozRequestFullScreen) {
  1441. element.mozRequestFullScreen();
  1442. } else if (element.webkitRequestFullscreen) {
  1443. this._scroll = {'x' : window.scrollX, 'y' : window.scrollY}
  1444. element.webkitRequestFullscreen();
  1445. } else if (element.msRequestFullscreen) {
  1446. element.msRequestFullscreen();
  1447. } else {
  1448. // If no fullscreen on div is supported:
  1449. this.toggleMaximize();
  1450. return;
  1451. }
  1452. this.container.classList.add('swmp-fullscreen');
  1453. }
  1454. }
  1455.  
  1456. }
  1457.  
  1458. toggleMaximize = (event) => {
  1459. if (event) {
  1460. event.preventDefault();
  1461. }
  1462.  
  1463. // Remove fullscreen if toggle off maximize in fullscreen
  1464. const fullscreenElement =
  1465. document.fullscreenElement ||
  1466. document.mozFullScreenElement ||
  1467. document.webkitFullscreenElement ||
  1468. document.msFullscreenElement;
  1469. if (fullscreenElement) {
  1470. if (document.exitFullscreen) {
  1471. document.exitFullscreen();
  1472. } else if (document.mozCancelFullScreen) {
  1473. document.mozCancelFullScreen();
  1474. } else if (document.webkitExitFullscreen) {
  1475. window.scrollTo({
  1476. top: this._scroll['y'],
  1477. left: this._scroll['x'],
  1478. behavior: 'auto'
  1479. });
  1480. document.webkitExitFullscreen(); //Safari sucks
  1481. } else if (document.msExitFullscreen) {
  1482. document.msExitFullscreen();
  1483. }
  1484. this.container.classList.remove('swmp-fullscreen');
  1485. this.container.classList.remove('swmp-maximized');
  1486. return;
  1487. }
  1488.  
  1489. if (this.type == 'audio' && this.playlist == null) { //allow on playlist, disallow on audio only
  1490. this.container.classList.remove('swmp-maximized');
  1491. return;
  1492. }
  1493. this.container.classList.toggle('swmp-maximized');
  1494. }
  1495.  
  1496. prepareSharedEvents() { // Redo on player change
  1497.  
  1498. this.playerContainer.addEventListener('click', (event) => {
  1499. this.togglePlay();
  1500. });
  1501.  
  1502. this.playerContainer.addEventListener('dblclick', (event) => {
  1503. if (swmpConfig.doubleclickMaximize == 'true') {
  1504. this.toggleMaximize();
  1505. return;
  1506. }
  1507.  
  1508. this.fullscreen(this.container);
  1509. });
  1510.  
  1511. }
  1512.  
  1513. preparePlayerEvents() {
  1514.  
  1515.  
  1516. this.player.onended = (event) => {
  1517. this.player.classList.remove('swmp-playing');
  1518. this.playbutton.classList.remove('swmp-playing');
  1519. this.seeker.value = 0;
  1520. this.progress.value = 0;
  1521. clearInterval(this.player.interval);
  1522. this.currentTimer.textContent = '00:00';
  1523. this.loaded = false;
  1524. if (this.playlist != undefined && swmpConfig.loop == 'false' && swmpConfig.autoplay == 'true') { //doesnt activate on loop
  1525. this.nextMedia();
  1526. return;
  1527. }
  1528. }
  1529.  
  1530. this.player.addEventListener('loadedmetadata', (event) => {
  1531. this.totalTimer.textContent = this.formatSeconds(this.player.duration);
  1532. });
  1533. this.player.addEventListener('loadeddata', (event) => {
  1534. // Video is loaded and can be played
  1535. this.loaded = true;
  1536. });
  1537.  
  1538.  
  1539. this.player.onplay = (event) => {
  1540. this.loaded = true;
  1541. this.player.classList.add('swmp-playing');
  1542. this.playbutton.classList.add('swmp-playing');
  1543. this.player.interval = window.setInterval((event) => {
  1544. //console.log('time');
  1545. this.player.timeupdate();
  1546. }, 40);
  1547. }
  1548.  
  1549. this.player.onpause = (event) => {
  1550. this.player.classList.remove('swmp-playing');
  1551. this.playbutton.classList.remove('swmp-playing');
  1552. clearInterval(this.player.interval);
  1553. }
  1554.  
  1555. this.player.onerror = (event) => {
  1556. clearInterval(this.player.interval);
  1557. var _error = this.player.error.message;
  1558. console.log(_error);
  1559. this.playerContainer.innerHTMl = _error;
  1560. }
  1561.  
  1562. this.player.timeupdate = (event) => {
  1563. this.seeker.value = Math.floor(this.player.currentTime / this.player.duration * this.seeker.max);
  1564. this.seeker.setAttribute("value", this.seeker.value);
  1565. this.progress.value = this.seeker.value;
  1566. this.progress.setAttribute("value", this.seeker.value);
  1567. this.updateTimer();
  1568. }
  1569.  
  1570. this.seeker.oninput = (event) => {
  1571. if (this.loaded != true) {
  1572. console.log('Userscript: Video status must be loaded first');
  1573. return;
  1574. }
  1575. try {
  1576. //on mousedown temporary add a mute to avoid annoying seeking sounds?
  1577. this.player.currentTime = (this.player.duration * this.seeker.value / this.seeker.max);
  1578. this.progress.value = this.seeker.value;
  1579. this.progress.setAttribute("value", this.seeker.value);
  1580. this.updateTimer();
  1581. } catch (e) {
  1582. console.log('Userscript: ' + e);
  1583. }
  1584. }
  1585.  
  1586. this.updateTimer = () => {
  1587. this.currentTimer.textContent = this.formatSeconds(this.player.currentTime);
  1588. //this.totalTimer.textContent = this.formatSeconds(this.player.duration);
  1589. }
  1590.  
  1591. this.seekBack = function() {
  1592. var time = this.player.currentTime;
  1593. var max = this.player.duration;
  1594. if (time < 5) {
  1595. time = 0;
  1596. } else {
  1597. time = time - 5;
  1598. }
  1599. this.player.currentTime = time;
  1600. this.updateTimer();
  1601. this.seeker.value = Math.floor(time / max * this.seeker.max);
  1602. this.seeker.setAttribute('value', this.seeker.value);
  1603. this.progress.value = this.seeker.value;
  1604. this.progress.setAttribute('value', this.seeker.value);
  1605. }
  1606.  
  1607. this.seekForward = function() {
  1608. var time = this.player.currentTime;
  1609. var max = this.player.duration;
  1610. if (time+5 >= max ) {
  1611. time = max;
  1612. } else {
  1613. time = time + 5;
  1614. }
  1615. this.player.currentTime = time;
  1616. this.updateTimer();
  1617.  
  1618. this.seeker.value = Math.floor(time / max * this.seeker.max);
  1619. this.seeker.setAttribute('value', this.seeker.value);
  1620. this.progress.value = this.seeker.value;
  1621. this.progress.setAttribute('value', this.seeker.value);
  1622.  
  1623. }
  1624.  
  1625. this.togglePlay = () => {
  1626. try {
  1627. if (this.player.paused ) {
  1628. this.player.play();
  1629. } else {
  1630. this.player.pause();
  1631. }
  1632. } catch (e) {
  1633. console.log('Userscript: ' + e)
  1634. }
  1635. }
  1636.  
  1637. this.toggleMute = () => {
  1638. if (this.player.muted) {
  1639. this.volumeButton.classList.remove('swmp-mute');
  1640. this.player.muted = false;
  1641. this._volume = this.player.volume;
  1642. this.volumeRange.setAttribute('value', this._volume * 100 );
  1643. this.volumeRange.value = this._volume * 100;
  1644. this.volumeProgress.setAttribute('value', this._volume * 100 );
  1645. this.volumeProgress.value = this._volume * 100;
  1646. swmpConfig.muted = 'false';
  1647. localStorage.swmpMuted = 'false';
  1648. } else {
  1649. this.volumeButton.classList.add('swmp-mute');
  1650. this.player.muted = true;
  1651. this.volumeRange.setAttribute('value', 0 );
  1652. this.volumeRange.value = 0;
  1653. this.volumeProgress.setAttribute('value', 0 );
  1654. this.volumeProgress.value = 0;
  1655. swmpConfig.muted = 'true';
  1656. localStorage.swmpMuted = 'true';
  1657. }
  1658. //console.log(this._volume);
  1659. }
  1660.  
  1661. this.updateVolume = (firstrun = false) => {
  1662.  
  1663. this.volumeButton.classList.remove('swmp-mute');
  1664.  
  1665. if (firstrun == true) {
  1666. this.volumeRange.setAttribute('value', this.defaultVolume);
  1667. this.volumeProgress.setAttribute('value', this.defaultVolume);
  1668. this.volumeButton.classList.add('swmp-min');
  1669. this._volume = this.defaultVolume;
  1670.  
  1671. }
  1672.  
  1673. if (this.player.muted) {
  1674. this.player.muted = false;
  1675. }
  1676.  
  1677. this.volumeRange.setAttribute('value', this.volumeRange.value );
  1678. this.volumeProgress.setAttribute('value', this.volumeRange.value );
  1679. this.player.volume = parseFloat(this.volumeRange.value / 100);
  1680. this._volume = this.player.volume;
  1681.  
  1682. if (this._volume > 0.50) {
  1683. this.volumeButton.classList.add('swmp-max');
  1684. this.volumeButton.classList.remove('swmp-med');
  1685. this.volumeButton.classList.remove('swmp-min');
  1686. } else if (this._volume > 0.10) {
  1687. this.volumeButton.classList.add('swmp-med');
  1688. this.volumeButton.classList.remove('swmp-max');
  1689. this.volumeButton.classList.remove('swmp-min');
  1690. } else if (this._volume > 0.05) {
  1691. this.volumeButton.classList.add('swmp-min');
  1692. this.volumeButton.classList.remove('swmp-max');
  1693. this.volumeButton.classList.remove('swmp-med');
  1694. }
  1695.  
  1696. //console.log(this._volume);
  1697.  
  1698. localStorage.swmpVolume = this.volumeRange.value;
  1699. swmpConfig.volume = this.volumeRange.value;
  1700. }
  1701.  
  1702. this.volumeRange.oninput = (event) => {
  1703. this.updateVolume();
  1704. }
  1705.  
  1706. this.player.addEventListener('volumechange', this.updateVolume() );
  1707. this.updateVolume(true); // First time Run
  1708. }
  1709.  
  1710. formatSeconds = (seconds) => {
  1711. this._sec_num = parseInt(seconds, 10);
  1712. this._hours = Math.floor(this._sec_num / 3600);
  1713. this._minutes = Math.floor(this._sec_num / 60) % 60;
  1714. this._seconds = this._sec_num % 60;
  1715.  
  1716. return [this._hours,this._minutes,this._seconds]
  1717. .map(v => v < 10 ? "0" + v : v)
  1718. .filter((v,i) => v !== "00" || i > 0)
  1719. .join(":");
  1720. }
  1721.  
  1722. removeClassByPrefix(el, prefix) {
  1723. let pattern = '(' + prefix + '(\\s|(-)?(\\w*)(\\s)?)).*?';
  1724. var regEx = new RegExp(pattern, 'g');
  1725. el.className = el.className.replace(regEx, '');
  1726. }
  1727.  
  1728. uuid() {
  1729. // Source: https://www.w3resource.com/javascript-exercises/javascript-math-exercise-23.php
  1730. var dt = new Date().getTime();
  1731. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  1732. var r = (dt + Math.random()*16)%16 | 0;
  1733. dt = Math.floor(dt/16);
  1734. return (c=='x' ? r :(r&0x3|0x8)).toString(16);
  1735. });
  1736. return uuid;
  1737. }
  1738.  
  1739. getFileName(url) {
  1740. return url.split('/').pop().split('#')[0].split('?')[0];
  1741. }
  1742.  
  1743. checkURL(url) {
  1744.  
  1745. // if match youtube regex then do this and return
  1746. var result = ytregex.exec(url);
  1747. //console.log(ytregex);
  1748. //console.log(url);
  1749. //console.log(result);
  1750. if (url.match(ytregex) ) {
  1751. console.log('SWMP: YouTube');
  1752. this.type = 'youtube';
  1753. this.videoid = result[1];
  1754. console.log('SWMP: Youtube ID: '+ this.videoid);
  1755. return true;
  1756. }
  1757.  
  1758. console.log("SWMP: Video or Audio");
  1759. // Check filename in URL for Type / MIME.
  1760. this.fileExt = url.split(/[#?&]/)[0].split('.').pop().trim();
  1761. this.fileExt = url.split(/[#?&]/).slice(-2)[0].split('.').pop().trim();
  1762. console.log(this.fileExt);
  1763. this.fileExt = this.fileExt.toLowerCase();
  1764.  
  1765. switch (this.fileExt) {
  1766. //VIDEO
  1767. case 'avi':
  1768. this.type = 'video';
  1769. this.mime = 'video/x-msvideo';
  1770. break;
  1771. case 'mpeg':
  1772. this.type = 'video';
  1773. this.mime = 'video/mpeg';
  1774. break;
  1775. case 'mpg':
  1776. this.type = 'video';
  1777. this.mime = 'video/mpeg';
  1778. break;
  1779. case 'ogv':
  1780. this.type = 'video';
  1781. this.mime = 'video/ogg';
  1782. break;
  1783. case 'mp4':
  1784. this.type = 'video';
  1785. this.mime = 'video/mp4';
  1786. break;
  1787. case 'webm':
  1788. this.type = 'video';
  1789. this.mime = 'video/webm';
  1790. break;
  1791. case 'flv':
  1792. this.type = 'video';
  1793. this.mime = 'video/x-flv';
  1794. break;
  1795. //AUDIO
  1796. case 'wav':
  1797. this.type = 'audio';
  1798. this.mime = 'audio/x-wav';
  1799. break;
  1800. case 'mp3':
  1801. this.type = 'audio';
  1802. this.mime = 'audio/mpeg';
  1803. break;
  1804. case 'm4a':
  1805. this.type = 'audio';
  1806. this.mime = 'audio/mp4';
  1807. break;
  1808. case 'mp2':
  1809. this.type = 'audio';
  1810. this.mime = 'audio/mpeg';
  1811. break;
  1812. case 'ogg':
  1813. this.type = 'audio';
  1814. this.mime = 'audio/ogg';
  1815. break;
  1816. case 'flac':
  1817. this.type = 'audio';
  1818. this.mime = 'audio/flac';
  1819. break;
  1820. default:
  1821. console.log('No matching ext/mime');
  1822. console.log(this.fileExt);
  1823. return false;
  1824. }
  1825. return true; //If valid
  1826. }
  1827.  
  1828. makeDraggable (element){
  1829. var elm = this.windowTitlebar;
  1830.  
  1831. element.style.position = 'fixed';
  1832. element.style.top = swmpConfig.positionTop + 'px';
  1833. if (swmpConfig.positionSide == 'right') {
  1834. element.style.right = swmpConfig.positionOffset + 'px';
  1835. } else {
  1836. element.style.left = swmpConfig.positionOffset + 'px';
  1837. }
  1838.  
  1839. var isMouseDown = false,
  1840. mouseX,
  1841. mouseY,
  1842. elmTop,
  1843. elmLeft,
  1844. diffX,
  1845. newElmTop,
  1846. newElmLeft,
  1847. diffY,
  1848. rightBarrier,
  1849. bottomBarrier;
  1850.  
  1851. function mouseDown(e) {
  1852.  
  1853. if (e.which == '3') {
  1854. return false; // Right click
  1855. }
  1856.  
  1857. element.focus();
  1858.  
  1859. isMouseDown = true;
  1860.  
  1861. mouseX = e.clientX;
  1862. mouseY = e.clientY;
  1863.  
  1864. elmTop = element.offsetTop;
  1865. elmLeft = element.offsetLeft;
  1866. diffX = mouseX - elmLeft;
  1867. diffY = mouseY - elmTop;
  1868.  
  1869. }
  1870.  
  1871. function mouseUp() {
  1872. isMouseDown = false;
  1873. if (swmpConfig.positionSide == 'right') {
  1874. var currentleft = parseInt(element.style.left, 10);
  1875. element.style.left = "unset";
  1876. element.style.right = document.documentElement.clientWidth - currentleft - element.offsetWidth + "px";
  1877. }
  1878. }
  1879.  
  1880. function mouseMove(e) {
  1881.  
  1882. if (!isMouseDown) return;
  1883.  
  1884. if (e.which != '1') {
  1885. isMouseDown = false;
  1886. }
  1887.  
  1888. if (element.classList.contains('swmp-maximized') ) return;
  1889. //element.classList.remove('swmp-maximized');
  1890. var newMouseX = e.clientX;
  1891. var newMouseY = e.clientY;
  1892.  
  1893. newElmTop = newMouseY - diffY;
  1894. newElmLeft = newMouseX - diffX;
  1895.  
  1896. rightBarrier = document.documentElement.clientWidth - element.offsetWidth;
  1897. bottomBarrier = window.innerHeight - element.offsetHeight;
  1898.  
  1899. if( ( newElmLeft < 0 ) || ( newElmTop < 0) || ( newElmLeft > rightBarrier ) || (newElmTop > bottomBarrier) ) {
  1900. if ( newElmLeft < 0 ) {
  1901. newElmLeft = 0;
  1902.  
  1903. }
  1904.  
  1905. if ( newElmTop < 0) {
  1906. newElmTop = 0;
  1907.  
  1908. }
  1909. if ( newElmLeft > rightBarrier ) {
  1910. newElmLeft = rightBarrier;
  1911.  
  1912. }
  1913. if (newElmTop > bottomBarrier) {
  1914. newElmTop = bottomBarrier;
  1915. }
  1916. }
  1917.  
  1918. element.style.top = newElmTop + "px";
  1919. element.style.bottom = "unset";
  1920.  
  1921. if (element.offsetTop < 0) {
  1922. element.style.top = "0px";
  1923. }
  1924. if (swmpConfig.positionSide == 'right') {
  1925. element.style.left = newElmLeft + "px";
  1926. element.style.right = "unset";
  1927. } else {
  1928. element.style.left = newElmLeft + "px";
  1929. }
  1930.  
  1931. }
  1932.  
  1933. document.addEventListener('mousemove', mouseMove);
  1934. document.addEventListener('mouseup', mouseUp);
  1935. elm.addEventListener('mousedown', mouseDown);
  1936. }
  1937.  
  1938. }
  1939.  
  1940. // Above this is SWMP class.
  1941.  
  1942.  
  1943. // Below this is the event listener that checks if what you are clicking on is a video or audio that should be put into SWMP.
  1944. // If you want to make site specific changes, either expand on it or just make a completely separate event listener below it.
  1945.  
  1946. document.addEventListener('click', (event) => {
  1947. //console.log("Userscript - "+event.target.tagName);
  1948.  
  1949. // If there are no links in sight, do nothing.
  1950. if (event.target.tagName != 'A' && event.target.parentNode.tagName != 'A') {
  1951. return;
  1952. }
  1953. // Lets figure out which one is the link. Wont normally chain As inside As so... Sometimes image may be inside span inside A so lets do 3x.
  1954. var clicked = false;
  1955. if (event.target.tagName == 'A') {
  1956. clicked = event.target;
  1957. } else if (event.target.parentNode.tagName == 'A') {
  1958. clicked = event.target.parentNode;
  1959. } else if (event.target.parentNode.parentNode.tagName == 'A') {
  1960. clicked = event.target.parentNode.parentNode;
  1961. }
  1962.  
  1963. // Download attribute override
  1964. if (swmpConfig.downloadAttribute == 'false' && clicked.hasAttribute('download')) {
  1965. return;
  1966. }
  1967. function getVideo() {
  1968. if (backendScript == '' || backendScript == null || backendScript == undefined || backendScript == 'fourchan') {
  1969. return clicked.getAttribute('href');
  1970. } else if (backendScript == 'vichan') {
  1971. //console.log("Userscript - "+clicked.parentNode.querySelectorAll('p.fileinfo a') );
  1972. var allthelinks = clicked.parentNode.querySelectorAll('p.fileinfo a'); // Prevent capturing the (Hide) link on some installs
  1973. var matchinglink = '';
  1974. for (var i = 0; i < allthelinks.length ; i++) {
  1975. if (allthelinks[i].getAttribute('href').match(fileregex) ) {
  1976. matchinglink = allthelinks[i];
  1977. }
  1978. }
  1979. if (matchinglink == '') { // Kissu New UI (no thanks, but here it is anyways) <- This should also return normal href if not video?
  1980. matchinglink = clicked;
  1981. }
  1982. //console.log("Userscript - "+matchinglink);
  1983. return matchinglink.getAttribute('href');
  1984. } else {
  1985. return clicked.getAttribute('href');
  1986. }
  1987. }
  1988. function getFileNameFromUrl(link) {
  1989. if (backendScript == '' || backendScript == null || backendScript == undefined || backendScript == 'fourchan') {
  1990. return link;
  1991. } else if (backendScript == 'vichan') {
  1992. return link;
  1993. } else {
  1994. return link;
  1995. }
  1996. }
  1997. function getTitle(link) {
  1998. if (backendScript == 'fourchan') {
  1999. if (link.parentNode.querySelector('div.fileText a') != null) {
  2000. if (link.parentNode.querySelector('div.fileText a').getAttribute('title') ) {
  2001. return link.parentNode.querySelector('div.fileText a').getAttribute('title');
  2002. } else {
  2003. return link.parentNode.querySelector('div.fileText a').innerText;
  2004. }
  2005. } else {
  2006. return link.getAttribute('href');
  2007. }
  2008. }
  2009. if (backendScript == 'vichan') { // https://regex101.com/r/HivDYB/3
  2010. let regex = new RegExp(`((?:[^\.|^\/|^\=|^\n|^\&])+(?:\.)(?:${swmpConfig.files}))(?:[^(\w)]|$)`, 'gmi'); // Tests for www.webmaster.com/test.webm domain too.
  2011. var newlink = [...link.matchAll(regex)]; // Remove query behind *final filename* &title for old filename on vichan will stay.
  2012. return newlink.slice(-1)[0][1]; // return last in array, grabs t= title for vichan player.php links.
  2013. }
  2014. }
  2015.  
  2016. var clickedHref;
  2017. var clickedFileName;
  2018. var clickedTitle;
  2019. var anythingmatch;
  2020.  
  2021. function isYoutube(link) {
  2022. var result = ytregex.exec(link);
  2023.  
  2024. if (link.match(ytregex) != null ) {
  2025. anythingmatch = true;
  2026. console.log('Userscript - YouTube: '+result[1]); // Video ID
  2027. clickedTitle = 'YouTube: ' + result[1];
  2028. return true;
  2029. } else {
  2030. console.log('Userscript - Not YouTube');
  2031. return false;
  2032. }
  2033. }
  2034.  
  2035. // If youtube
  2036.  
  2037. if (isYoutube(clicked.getAttribute('href') ) ) {
  2038. console.log('Userscript - YouTube link clicked');
  2039. clickedHref = clicked.getAttribute('href');
  2040. //clickedTitle = 'Loading YouTube';
  2041. }
  2042.  
  2043. function isVideo(link) {
  2044. var result = fileregex.exec(link);
  2045.  
  2046. if (link.match(fileregex) != null) {
  2047. anythingmatch = true;
  2048. console.log('Userscript - Video/Audio match:' + result);
  2049. return true;
  2050. } else {
  2051. console.log('Userscript - Not Video/Audio');
  2052. return false;
  2053. }
  2054. }
  2055.  
  2056. //get video links
  2057. clickedHref = getVideo();
  2058.  
  2059. if (isVideo(clickedHref) ) {
  2060. //clickedHref = getVideo();
  2061. clickedFileName = getFileNameFromUrl(clicked.getAttribute('href') );
  2062. clickedTitle = false;
  2063. if (backendScript == '' || backendScript == null || backendScript == undefined) {
  2064. clickedTitle = decodeURI(clickedFileName); //Change with other scripts later if it doesn't already contain the title.
  2065. } else if (backendScript == 'fourchan') {
  2066. clickedTitle = decodeURI(getTitle(clicked) );
  2067. } else if (backendScript == 'vichan') {
  2068. clickedTitle = getTitle(decodeURI(clicked.getAttribute('href') ) );
  2069. } else {
  2070. clickedTitle = decodeURI(clickedFileName); //Change with other scripts later if it doesn't already contain the title.
  2071. }
  2072. console.log("Userscript - URL: " + clicked.getAttribute('href') );
  2073. console.log("Userscript - Filename: " + clickedFileName );
  2074. console.log("Userscript - Title: " + clickedTitle);
  2075. }
  2076.  
  2077. if (anythingmatch != true) {
  2078. return false;
  2079. } else {
  2080. // If video audio or youtube matched, prevent other events.
  2081. event.preventDefault();
  2082. event.stopPropagation();
  2083. }
  2084. // todo:
  2085. // toggleable check if has download attribute?
  2086. // add Q/E shortcut for prev/next <- yt bugs on fast move
  2087.  
  2088. // Check if ctrl-shift-click and scan all links for yt/media, get titles,
  2089. // remove player.php + & tag from vichan to not get different url from link and thumb
  2090. // strip duplicates, feed to playlist WITH title
  2091. // Guess I'll need the ability to push urls+title to the player,
  2092. // lets send ["url", "title"] also adapt title get on playlist to grab from playlist
  2093.  
  2094.  
  2095. // Okay, time to load the player.
  2096. var playerid = false;
  2097.  
  2098. if (swmpConfig.allowMultiple != 'false') {
  2099. playerid = `play-swmp-${clicked.getAttribute('href')}`;
  2100. } else {
  2101. playerid = 'play-swmp';
  2102. }
  2103.  
  2104. if (typeof(document.getElementById(playerid)) != 'undefined' && document.getElementById(playerid) != null) {
  2105. if (swmpConfig.allowMultiple != 'false') {
  2106. return false; //already exists, lets do nothing.
  2107. } else {
  2108. document.getElementById(playerid).remove();
  2109. //already exists, lets get rid of it.
  2110. }
  2111. }
  2112. //console.log(playerid+clickedHref+clickedTitle);
  2113. let newembed = new swmp({
  2114. id: playerid,
  2115. url: clickedHref,
  2116. title: clickedTitle
  2117. });
  2118. //console.log(backendScript);
  2119. if (backendScript == 'jschan') { // Might be useful for inline.
  2120. clicked.parentNode.parentNode.appendChild(newembed.container);
  2121. } else {
  2122. clicked.parentNode.appendChild(newembed.container);
  2123. }
  2124.  
  2125. // Give focus to player for keybinds.
  2126. newembed.container.focus();
  2127.  
  2128.  
  2129. }, true); // Fuck other scripts.