Plex External Player

Play plex videos in an external player

当前为 2016-02-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Plex External Player
  3. // @namespace https://github.com/Kayomani/PlexExternalPlayer
  4. // @version 1.4
  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. // @require https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js
  11. // @grant GM_xmlhttpRequest
  12. // ==/UserScript==
  13.  
  14. $("head").append (
  15. '<link href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet" type="text/css">'
  16. );
  17.  
  18.  
  19. toastr.options = {
  20. "closeButton": true,
  21. "debug": false,
  22. "newestOnTop": true,
  23. "progressBar": true,
  24. "positionClass": "toast-bottom-right",
  25. "preventDuplicates": false,
  26. "onclick": null,
  27. "showDuration": "300",
  28. "hideDuration": "1000",
  29. "timeOut": "5000",
  30. "extendedTimeOut": "1000",
  31. "showEasing": "swing",
  32. "hideEasing": "linear",
  33. "showMethod": "fadeIn",
  34. "hideMethod": "fadeOut"
  35. };
  36. var showToast = function(msg, error){
  37. var title = 'Plex External Player';
  38. if(error){
  39. toastr.error(msg, title, {timeOut: 10000});
  40. logMessage(msg);
  41. } else {
  42. toastr.success(msg, title);
  43. }
  44. };
  45.  
  46. var logMessage = function(msg){
  47. console.log('Plex External: ' + msg);
  48. };
  49.  
  50. var makeRequest = function(url){
  51. return new Promise( function (resolve, reject) {
  52. GM_xmlhttpRequest({
  53. method: "GET",
  54. headers: {
  55. "X-Plex-Token":localStorage["myPlexAccessToken"]
  56. },
  57. url: url,
  58. onload: resolve,
  59. onreadystatechange: function(state) {
  60. if (state.readyState === 4) {
  61. if (state.status !== 200) {
  62. showToast('Error calling: ' + url + '. Response: ' + error.responseText + ' Code:' + error.status + ' Message: ' + error.statusText, 1);
  63. }
  64. }
  65. },
  66. onerror: reject
  67. });
  68. });
  69. };
  70.  
  71.  
  72.  
  73. var markAsPlayedInPlex = function(id) {
  74. logMessage('Marking ' + id + ' as played');
  75. return makeRequest(window.location.origin + '/:/scrobble?key='+ id +'&identifier=com.plexapp.plugins.library').catch(function(){
  76. showToast('Failed to mark item ' + id + ' as played');
  77. });
  78. };
  79.  
  80. var openItemOnAgent = function(path, id, openFolder) {
  81. if(openFolder){
  82. var fwd = path.lastIndexOf('/');
  83. var bck = path.lastIndexOf('\\');
  84. var best = fwd>bck?fwd:bck;
  85. if(best>-1){
  86. path = path.substr(0, best);
  87. }
  88. }
  89. showToast('Playing ' + path, 0);
  90. logMessage('Playing ' + path);
  91. var url = 'http://localhost:7251/?protocol=1&item=' + encodeURIComponent(path);
  92. return new Promise(function (resolve, reject) {
  93. makeRequest(url).then(function(){
  94. markAsPlayedInPlex(id).then(resolve, reject);
  95. },reject);
  96. });
  97. };
  98.  
  99. var clickListener = function(e) {
  100. e.preventDefault();
  101. e.stopPropagation();
  102. var a = jQuery(e.target).closest('a');
  103. var link = a.attr('href');
  104. var openFolder = a.attr('data-type') === 'folder';
  105. var url = link;
  106. if (link === '#' || link === undefined) {
  107. url = window.location.hash;
  108. }
  109.  
  110. if (url.indexOf('%2Fmetadata%2F') > -1) {
  111. var idx = url.indexOf('%2Fmetadata%2F');
  112. var id = url.substr(idx + 14);
  113.  
  114. // Get metadata
  115. var metaDataPath = window.location.origin + '/library/metadata/' + id + '?checkFiles=1&includeExtras=1';
  116. makeRequest(metaDataPath)
  117. .then(function(response){
  118. // Play the first availible part
  119. var parts = response.responseXML.getElementsByTagName('Part');
  120. for (var i = 0; i < parts.length; i++) {
  121. if (parts[i].attributes['file'] !== undefined) {
  122. openItemOnAgent(parts[i].attributes['file'].value, id, openFolder).catch(function(){
  123. showToast('Failed to connect to agent, is it running or firewalled?',1);
  124. });
  125. return;
  126. }
  127. }
  128.  
  129. if (parts.length === 0) {
  130. // If we got a directory/Season back then get the files in it
  131. var dirs = response.responseXML.getElementsByTagName('Directory');
  132. if (dirs.length > 0) {
  133. makeRequest(window.location.origin + dirs[0].attributes['key'].value)
  134. .then(function(response){
  135. var videos = response.responseXML.getElementsByTagName('Video');
  136. var file = null;
  137. var id = null;
  138. for (var i = 0; i < videos.length; i++) {
  139. var vparts = videos[i].getElementsByTagName('Part');
  140. if (vparts.length > 0) {
  141. file = vparts[0].attributes['file'].value;
  142. id = vparts[0].attributes['id'].value;
  143. if (videos[i].attributes['lastViewedAt'] === null || videos[i].attributes['lastViewedAt'] === undefined) {
  144. break;
  145. }
  146. }
  147. }
  148.  
  149. if (file !== null) {
  150. openItemOnAgent(file, id, openFolder).catch(function(){
  151. showToast('Failed to connect to agent, is it running or firewalled?',1);
  152. });
  153. }
  154. }).catch(function(){
  155. showToast('Failed to get information for directory',1);
  156. });
  157. }
  158. }
  159. }, function(error){
  160. showToast('Error getting metadata from' + metaDataPath, 1);
  161. });
  162. }
  163. };
  164.  
  165. var bindClicks = function() {
  166. jQuery(".glyphicon.play").each(function(i, e) {
  167. e = jQuery(e);
  168. if (!e.hasClass('plexextplayer')) {
  169. if (!e.parent().hasClass('hidden')) {
  170. e.addClass('plexextplayer');
  171. var parent = e.parent().parent();
  172. if (parent.is('li')) {
  173. var template = jQuery('<li><a class="btn-gray" href="#" title="Play Externally" data-toggle="Play Externally" data-original-title="Play Externally"><i class="glyphicon play plexextplayer plexextplayerico"></i></a></li><li><a class="btn-gray" href="#" title="Open containing folder" data-type="folder" data-toggle="Play Externally" data-original-title="Open containing folder"><i class="glyphicon play plexextplayer plexfolderextplayerico"></i></a></li>');
  174. parent.after(template);
  175. template.click(clickListener);
  176. } else if (parent.is('div') && parent.hasClass('media-poster-actions')) {
  177. var template = jQuery('<button class="play-btn media-poster-btn btn-link" tabindex="-1"><i class="glyphicon play plexextplayer plexextplayerico"></i></button>');
  178. parent.prepend(template);
  179. template.click(clickListener);
  180. }
  181. }
  182. }
  183. });
  184. };
  185.  
  186. // Make buttons smaller
  187. jQuery('body').append('<style>.media-poster-btn { padding: 8px !important; } .glyphicon.plexfolderextplayerico:before { content: "\\e343"; } .glyphicon.plexextplayerico:before { content: "\\e161"; }</style>');
  188.  
  189. // Bind buttons and check for new ones every 100ms
  190. setInterval(bindClicks, 100);
  191. bindClicks();