Plex External Player

Play plex videos in an external player

当前为 2017-03-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Plex External Player
  3. // @namespace https://github.com/Kayomani/PlexExternalPlayer
  4. // @version 1.10
  5. // @description Play plex videos in an external player
  6. // @author Kayomani
  7. // @include /^https?://.*:32400/web.*
  8. // @include http://*:32400/web/index.html*
  9. // @require http://code.jquery.com/jquery-1.11.3.min.js
  10. // @connect *
  11. // @require https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js
  12. // @grant GM_xmlhttpRequest
  13. // ==/UserScript==
  14.  
  15. $("head").append (
  16. '<link href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet" type="text/css">'
  17. );
  18.  
  19.  
  20. toastr.options = {
  21. "closeButton": true,
  22. "debug": false,
  23. "newestOnTop": true,
  24. "progressBar": true,
  25. "positionClass": "toast-bottom-right",
  26. "preventDuplicates": false,
  27. "onclick": null,
  28. "showDuration": "300",
  29. "hideDuration": "1000",
  30. "timeOut": "5000",
  31. "extendedTimeOut": "1000",
  32. "showEasing": "swing",
  33. "hideEasing": "linear",
  34. "showMethod": "fadeIn",
  35. "hideMethod": "fadeOut"
  36. };
  37.  
  38. var showToast = function(msg, error){
  39. var title = 'Plex External Player';
  40. if(error){
  41. toastr.error(msg, title, {timeOut: 10000});
  42. logMessage(msg);
  43. }
  44. else
  45. {
  46. toastr.success(msg, title);
  47. }
  48. };
  49.  
  50. var logMessage = function(msg){
  51. console.log('Plex External: ' + msg);
  52. };
  53.  
  54. var makeRequest = function(url, user, server){
  55. return new Promise( function (resolve, reject) {
  56. var origAccessToken = localStorage.myPlexAccessToken;
  57. var serverNode = JSON.parse(localStorage.users);
  58. var tokenToTry = origAccessToken;
  59. if(serverNode===undefined)
  60. {
  61. serverNode = {
  62. users : []
  63. };
  64. }
  65.  
  66. if(user!==undefined && server !==undefined)
  67. {
  68. if(user < serverNode.users.length)
  69. {
  70. if(server < serverNode.users[user].servers.length)
  71. {
  72. tokenToTry = serverNode.users[user].servers[server].accessToken;
  73. }
  74. else
  75. {
  76. showToast('Could not find authentication info', 1);
  77. reject();
  78. return;
  79. }
  80. }
  81. else
  82. {
  83. showToast('Could not find authentication info', 1);
  84. reject();
  85. return;
  86. }
  87. }
  88. var onError = function()
  89. {
  90. if(user===undefined)
  91. {
  92. user = 0;
  93. server = 0;
  94. } else
  95. {
  96. server++;
  97. if(serverNode.users[user].servers.length===server)
  98. {
  99. user++;
  100. server = 0;
  101. }
  102. }
  103. makeRequest(url,user,server).then(resolve, reject);
  104. };
  105. console.log('[Plex External] Calling ' + url);
  106. console.log('[Plex External] Using clientId ' + localStorage.clientID);
  107. console.log('[Plex External] Using Token ' + tokenToTry);
  108.  
  109. GM_xmlhttpRequest({
  110. method: "GET",
  111. headers: headers = {
  112. "X-Plex-Client-Identifier":localStorage.clientID,
  113. "X-Plex-Token":tokenToTry
  114.  
  115. },
  116. url: url,
  117. onload: function(state){
  118. if (state.status === 200) {
  119. console.log('[Plex External] Plex called successfully');
  120. resolve(state);
  121. }
  122. },
  123. onreadystatechange: function(state) {
  124. if (state.readyState === 4) {
  125. console.log('[Plex External] Not Authorised ' + url);
  126. if(state.status === 401)
  127. {
  128. onError();
  129. } else if (state.status !== 200) {
  130. showToast('Error calling: ' + url + '. Response: ' + state.responseText + ' Code:' + state.status + ' Message: ' + state.statusText, 1);
  131. }
  132. }
  133. },
  134. onerror: onError
  135. });
  136. });
  137. };
  138.  
  139.  
  140.  
  141. var markAsPlayedInPlex = function(id) {
  142. logMessage('Marking ' + id + ' as played');
  143. return makeRequest(window.location.origin + '/:/scrobble?key='+ id +'&identifier=com.plexapp.plugins.library').catch(function(){
  144. showToast('Failed to mark item ' + id + ' as played');
  145. });
  146. };
  147.  
  148. var openItemOnAgent = function(path, id, openFolder) {
  149. if(openFolder){
  150. var fwd = path.lastIndexOf('/');
  151. var bck = path.lastIndexOf('\\');
  152. var best = fwd>bck?fwd:bck;
  153. if(best>-1){
  154. path = path.substr(0, best);
  155. }
  156. }
  157. showToast('Playing ' + path, 0);
  158. logMessage('Playing ' + path);
  159. // umicrosharp doesn't handle plus properly
  160. path = path.replace(/\+/g, '[PLEXEXTPLUS]');
  161. var url = 'http://127.0.0.1:7251/?protocol=2&item=' + encodeURIComponent(path);
  162. return new Promise(function (resolve, reject) {
  163. makeRequest(url).then(function(){
  164. markAsPlayedInPlex(id).then(resolve, reject);
  165. },reject);
  166. });
  167. };
  168.  
  169. var clickListener = function(e) {
  170. e.preventDefault();
  171. e.stopPropagation();
  172. var a = jQuery(e.target).closest('a');
  173. var link = a.attr('href');
  174. var openFolder = jQuery(e.target).attr('data-type') === 'folder';
  175. var url = link;
  176. if (link === '#' || link === undefined || link === 'javascript:void(0)') {
  177. url = window.location.hash;
  178. }
  179.  
  180. if (url.indexOf('%2Fmetadata%2F') > -1) {
  181. var idx = url.indexOf('%2Fmetadata%2F');
  182. var id = url.substr(idx + 14);
  183.  
  184. // Get metadata
  185. var metaDataPath = window.location.origin + '/library/metadata/' + id + '?checkFiles=1&includeExtras=1';
  186. makeRequest(metaDataPath)
  187. .then(function(response){
  188. // Play the first availible part
  189. var parts = response.responseXML.getElementsByTagName('Part');
  190. for (var i = 0; i < parts.length; i++) {
  191. if (parts[i].attributes['file'] !== undefined) {
  192. openItemOnAgent(parts[i].attributes['file'].value, id, openFolder).catch(function(){
  193. showToast('Failed to connect to agent, is it running or firewalled?',1);
  194. });
  195. return;
  196. }
  197. }
  198.  
  199. if (parts.length === 0) {
  200. // If we got a directory/Season back then get the files in it
  201. var dirs = response.responseXML.getElementsByTagName('Directory');
  202. if (dirs.length > 0) {
  203. makeRequest(window.location.origin + dirs[0].attributes['key'].value)
  204. .then(function(response){
  205. var videos = response.responseXML.getElementsByTagName('Video');
  206. var file = null;
  207. var id = null;
  208. if(videos.length === 0)
  209. {
  210. showToast('Could not determine which video to play as there are multiple seasons.',true);
  211. return;
  212. }
  213. for (var i = 0; i < videos.length; i++) {
  214. var vparts = videos[i].getElementsByTagName('Part');
  215. if (vparts.length > 0) {
  216. file = vparts[0].attributes['file'].value;
  217. id = vparts[0].attributes['id'].value;
  218. if (videos[i].attributes['lastViewedAt'] === null || videos[i].attributes['lastViewedAt'] === undefined) {
  219. break;
  220. }
  221. }
  222. }
  223.  
  224. if (file !== null)
  225. {
  226. openItemOnAgent(file, id, openFolder).catch(function(){
  227. showToast('Failed to connect to agent, is it running or firewalled?',1);
  228. });
  229. }
  230. }).catch(function(){
  231. showToast('Failed to get information for directory',1);
  232. });
  233. }
  234. }
  235. }, function(error){
  236. showToast('Error getting metadata from ' + metaDataPath + "Error: " + error, 1);
  237. console.log('[Plex External] Error ' + JSON.stringify(error));
  238. });
  239. }
  240. };
  241.  
  242. var bindClicks = function() {
  243. var hasBtn = false;
  244. var toolBar= jQuery(".plex-icon-toolbar-play-560").parent().parent();
  245. toolBar.children('button').each(function(i, e) {
  246. if(jQuery(e).hasClass('plexextplayer'))
  247. hasBtn = true;
  248. });
  249.  
  250.  
  251. if(!hasBtn)
  252. {
  253. var template = jQuery('<button class="play-btn media-poster-btn btn-link plexextplayer" tabindex="-1" title="Play Externally"><i class="glyphicon play plexextplayer plexextplayerico"></i></button><button class="play-btn media-poster-btn btn-link plexextplayer" title="Open folder" tabindex="-1"><i data-type="folder" class="glyphicon play plexextplayer plexfolderextplayerico"></i></button>');
  254. toolBar.prepend(template);
  255. template.click(clickListener);
  256.  
  257. }
  258.  
  259. // Cover page
  260. jQuery(".plex-icon-more-560").each(function(i, e) {
  261. e = jQuery(e);
  262. var poster = e.parent().parent();
  263. if(poster.length === 1 && poster[0].className.startsWith('MetadataPosterCardOverlay'))
  264. {
  265. var existingButton = poster.find('.plexextplayerico');
  266. if(existingButton.length === 0)
  267. {
  268. var url = poster.find('a').attr('href');
  269. var template = jQuery('<a href="'+ url +'" aria-haspopup="false" aria-role="button" class="" type="button"><i class="glyphicon play plexextplayer plexextplayerico plexextplayericocover"></i></button>');
  270. var newButton = template.appendTo(poster);
  271. newButton.click(clickListener);
  272. poster.mouseenter(function(){
  273. newButton.find('i').css('display','block');
  274. });
  275. poster.mouseleave(function(){
  276. newButton.find('i').css('display','none');
  277. });
  278. }
  279. }
  280. });
  281. };
  282.  
  283. // Make buttons smaller
  284. jQuery('body').append('<style>.plexextplayericocover {right: 10px; top: 10px; position:absolute; display:none;font-size:15px;} .glyphicon.plexfolderextplayerico:before { content: "\\e145"; } .glyphicon.plexextplayerico:before { content: "\\e161"; }</style>');
  285.  
  286. // Bind buttons and check for new ones every 100ms
  287. setInterval(bindClicks, 100);
  288. bindClicks();