4chan \ IB Simple Web Media Player

Simple Web Media Player for 4chan and other imageboards.

当前为 2022-08-30 提交的版本,查看 最新版本

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