Greasy Fork is available in English.

Video Url Parser

urlParser to get extract information like provider, videoId and other from urls

נכון ליום 30-12-2014. ראה הגרסה האחרונה.

אין להתקין סקריפט זה ישירות. זוהי ספריה עבור סקריפטים אחרים // @require https://update.greatest.deepsurf.us/scripts/2859/29929/Video%20Url%20Parser.js

  1. // ==UserScript==
  2. // @name Video Url Parser
  3. // @namespace Zod-
  4. // @description urlParser to get extract information like provider, videoId and other from urls
  5. // @source https://github.com/Zod-/jsVideoUrlParser
  6. // @copyright Zod-
  7. // @version 0.3
  8. // @license MIT
  9. // ==/UserScript==
  10. function URLParser() {
  11. "use strict";
  12. this.plugins = {};
  13. }
  14. URLParser.prototype.parse = function(url) {
  15. "use strict";
  16. var th = this,
  17. match = url.match(/(?:https?:\/\/)?(?:[^\.]+\.)?(\w+)\./i),
  18. provider = match ? match[1] : undefined,
  19. result,
  20. createdUrl;
  21. if (match && provider && th.plugins[provider]) {
  22. result = th.plugins[provider].parse.call(this, url);
  23. if (result) {
  24. result.provider = th.plugins[provider].provider;
  25. return result;
  26. }
  27. }
  28. return undefined;
  29. };
  30. URLParser.prototype.bind = function(plugin) {
  31. var th = this;
  32. th.plugins[plugin.provider] = plugin;
  33. if (plugin.alternatives) {
  34. var i;
  35. for (i = 0; i < plugin.alternatives.length; i += 1) {
  36. th.plugins[plugin.alternatives[i]] = plugin;
  37. }
  38. }
  39. };
  40. URLParser.prototype.create = function(op) {
  41. var th = this,
  42. vi = op.videoInfo;
  43. op.format = op.format || 'short';
  44. if (th.plugins[vi.provider].create) {
  45. return th.plugins[vi.provider].create.call(this, op);
  46. }
  47. return undefined;
  48. };
  49.  
  50. window.urlParser = new URLParser();
  51.  
  52. //parses strings like 1h30m20s to seconds
  53. window.getTime = function(timeString) {
  54. "use strict";
  55. var totalSeconds = 0,
  56. timeValues = {
  57. 's': 1,
  58. 'm': 1 * 60,
  59. 'h': 1 * 60 * 60,
  60. 'd': 1 * 60 * 60 * 24,
  61. 'w': 1 * 60 * 60 * 24 * 7
  62. },
  63. timePairs;
  64.  
  65. //is the format 1h30m20s etc
  66. if (!timeString.match(/^(\d+[smhdw]?)+$/)) {
  67. return 0;
  68. }
  69. //expand to "1 h 30 m 20 s" and split
  70. timeString= timeString.replace(/([smhdw])/g, ' $1 ').trim();
  71. timePairs = timeString.split(' ');
  72.  
  73. for (var i = 0; i < timePairs.length; i += 2) {
  74. totalSeconds += parseInt(timePairs[i], 10) * timeValues[timePairs[i + 1] || 's'];
  75. }
  76. return totalSeconds;
  77. }
  78. //http://joquery.com/2012/string-format-for-javascript
  79. if (typeof String.prototype.format !== 'function') {
  80. String.prototype.format = function() {
  81. // The string containing the format items (e.g. "{0}")
  82. // will and always has to be the first argument.
  83. var theString = this,
  84. i,
  85. regEx;
  86.  
  87. // start with the second argument (i = 1)
  88. for (i = 0; i < arguments.length; i += 1) {
  89. // "gm" = RegEx options for Global search (more than one instance)
  90. // and for Multiline search
  91. regEx = new RegExp("\\{" + (i) + "\\}", "gm");
  92. theString = theString.replace(regEx, arguments[i]);
  93. }
  94. return theString;
  95. };
  96. }
  97. urlParser.bind({
  98. 'provider': 'dailymotion',
  99. 'alternatives': ['dai'],
  100. 'parse': function(url) {
  101. "use strict";
  102. var match,
  103. videoId,
  104. startTime,
  105. result = {};
  106.  
  107. match = url.match(/(?:\/video|ly)\/([A-Za-z0-9]+)/i);
  108. videoId = match ? match[1] : undefined;
  109.  
  110. match = url.match(/[#\?&]start=([A-Za-z0-9]+)/i);
  111. startTime = match ? getTime(match[1]) : undefined;
  112.  
  113. if (!videoId) {
  114. return undefined;
  115. }
  116. result.mediaType = 'video';
  117. result.videoId = videoId;
  118. if (startTime) {
  119. result.startTime = startTime;
  120. }
  121. return result;
  122. },
  123. 'create': function(op) {
  124. "use strict";
  125. var vi = op.videoInfo;
  126. if (vi.startTime) {
  127. return 'https://www.dailymotion.com/video/{0}?start={1}'.format(vi.videoId, vi.startTime);
  128. }
  129.  
  130. if (op.format === 'short') {
  131. return 'https://dai.ly/{0}'.format(vi.videoId);
  132. }
  133.  
  134. return 'https://www.dailymotion.com/video/{0}'.format(vi.videoId);
  135. }
  136. });
  137.  
  138. //not finished
  139. urlParser.bind({
  140. 'provider': 'livestream',
  141. 'parse': function(url) {
  142. "use strict";
  143. var match,
  144. channel;
  145. match = url.match(/livestream\.com\/(\w+)/i);
  146. channel = match ? match[1] : undefined;
  147. if (!channel) {
  148. return undefined;
  149. }
  150.  
  151. return {
  152. 'mediaType': 'stream',
  153. 'channel': channel
  154. };
  155. }
  156. });
  157. urlParser.bind({
  158. 'provider': 'twitch',
  159. 'parse': function(url) {
  160. "use strict";
  161. var match,
  162. videoId,
  163. channel,
  164. videoIdPrefix,
  165. result = {};
  166.  
  167. match = url.match(/twitch\.tv\/(\w+)(?:\/(.)\/(\d+))?/i);
  168. channel = match ? match[1] : undefined;
  169. videoIdPrefix = match ? match[2] : undefined;
  170. videoId = match ? match[3] : undefined;
  171.  
  172. match = url.match(/(?:\?channel|\&utm_content)=(\w+)/i);
  173. channel = match ? match[1] : channel;
  174.  
  175. if (!channel) {
  176. return undefined;
  177. }
  178. if (videoId) {
  179. result.mediaType = 'video';
  180. result.videoId = videoId;
  181. result.videoIdPrefix = videoIdPrefix;
  182. } else {
  183. result.mediaType = 'stream';
  184. }
  185. result.channel = channel;
  186.  
  187. return result;
  188. },
  189. 'create': function(videoInfo) {
  190. "use strict";
  191. var url,
  192. vi = op.videoInfo;
  193. if (vi.mediaType === 'stream') {
  194. return 'https://twitch.tv/{0}'.format(vi.channel);
  195. }
  196.  
  197. return 'https://twitch.tv/{0}/{1}/{2}'.format(vi.channel, vi.videoIdPrefix, vi.videoId);
  198. }
  199. });
  200. urlParser.bind({
  201. 'provider': 'vimeo',
  202. 'alternatives': ['vimeopro'],
  203. 'parse': function(url) {
  204. "use strict";
  205. var match,
  206. videoId;
  207. match = url.match(/(?:\/(?:channels\/[\w]+|(?:album\/\d+\/)?videos?))?\/(\d+)/i);
  208. videoId = match ? match[1] : undefined;
  209. if (!videoId) {
  210. return undefined;
  211. }
  212. return {
  213. 'mediaType': 'video',
  214. 'videoId': videoId
  215. };
  216. },
  217. 'create': function(videoInfo) {
  218. "use strict";
  219. return 'https://vimeo.com/{0}'.format(op.videoInfo.videoId);
  220. }
  221. });
  222. urlParser.bind({
  223. 'provider': 'youtube',
  224. 'alternatives': ['youtu'],
  225. 'parse': function(url) {
  226. "use strict";
  227. var match,
  228. videoId,
  229. playlistId,
  230. playlistIndex,
  231. startTime,
  232. result = {};
  233.  
  234. match = url.match(/(?:(?:v|be|videos)\/|v=)([\w\-]{11})/i);
  235. videoId = match ? match[1] : undefined;
  236.  
  237. match = url.match(/list=([\w\-]+)/i);
  238. playlistId = match ? match[1] : undefined;
  239.  
  240. match = url.match(/index=(\d+)/i);
  241. playlistIndex = match ? Number(match[1]) : undefined;
  242.  
  243. match = url.match(/[#\?&](?:star)?t=([A-Za-z0-9]+)/i);
  244. startTime = match ? getTime(match[1]) : undefined;
  245.  
  246. if (videoId) {
  247. result.mediaType = 'video';
  248. result.videoId = videoId;
  249. if (playlistId) {
  250. result.playlistId = playlistId;
  251. if (playlistIndex) {
  252. result.playlistIndex = playlistIndex;
  253. }
  254. }
  255. if (startTime) {
  256. result.startTime = startTime;
  257. }
  258. } else if (playlistId) {
  259. result.mediaType = 'playlist';
  260. result.playlistId = playlistId;
  261. } else {
  262. return undefined;
  263. }
  264.  
  265. return result;
  266. },
  267. 'create': function(op) {
  268. "use strict";
  269. var url,
  270. vi = op.videoInfo;
  271. if (vi.mediaType === 'playlist') {
  272. return 'https://www.youtube.com/playlist?feature=share&list={0}'.format(vi.playlistId);
  273. }
  274.  
  275. if (vi.playlistId) {
  276. url = 'https://www.youtube.com/watch?v={0}&list={1}'.format(vi.videoId, vi.playlistId);
  277. if (vi.playlistIndex) {
  278. url += '&index={0}'.format(vi.playlistIndex);
  279. }
  280. } else {
  281. if (op.format === 'short') {
  282. url = 'https://youtu.be/{0}'.format(vi.videoId);
  283. } else {
  284. url = 'https://www.youtube.com/watch?v={0}'.format(vi.videoId);
  285. }
  286. }
  287.  
  288. if (vi.startTime) {
  289. url += '#t={0}'.format(vi.startTime);
  290. }
  291. return url;
  292. }
  293. });