Plex External Player

Play plex videos in an external player

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

  1. // ==UserScript==
  2. // @name Plex External Player
  3. // @namespace https://github.com/Kayomani/PlexExternalPlayer
  4. // @version 1.5
  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. // umicrosharp doesn't handle plus properly
  92. path = path.replace(/\+/g, '[PLEXEXTPLUS]');
  93. var url = 'http://localhost:7251/?protocol=2&item=' + encodeURIComponent(path);
  94. return new Promise(function (resolve, reject) {
  95. makeRequest(url).then(function(){
  96. markAsPlayedInPlex(id).then(resolve, reject);
  97. },reject);
  98. });
  99. };
  100.  
  101. var clickListener = function(e) {
  102. e.preventDefault();
  103. e.stopPropagation();
  104. var a = jQuery(e.target).closest('a');
  105. var link = a.attr('href');
  106. var openFolder = a.attr('data-type') === 'folder';
  107. var url = link;
  108. if (link === '#' || link === undefined) {
  109. url = window.location.hash;
  110. }
  111.  
  112. if (url.indexOf('%2Fmetadata%2F') > -1) {
  113. var idx = url.indexOf('%2Fmetadata%2F');
  114. var id = url.substr(idx + 14);
  115.  
  116. // Get metadata
  117. var metaDataPath = window.location.origin + '/library/metadata/' + id + '?checkFiles=1&includeExtras=1';
  118. makeRequest(metaDataPath)
  119. .then(function(response){
  120. // Play the first availible part
  121. var parts = response.responseXML.getElementsByTagName('Part');
  122. for (var i = 0; i < parts.length; i++) {
  123. if (parts[i].attributes['file'] !== undefined) {
  124. openItemOnAgent(parts[i].attributes['file'].value, id, openFolder).catch(function(){
  125. showToast('Failed to connect to agent, is it running or firewalled?',1);
  126. });
  127. return;
  128. }
  129. }
  130.  
  131. if (parts.length === 0) {
  132. // If we got a directory/Season back then get the files in it
  133. var dirs = response.responseXML.getElementsByTagName('Directory');
  134. if (dirs.length > 0) {
  135. makeRequest(window.location.origin + dirs[0].attributes['key'].value)
  136. .then(function(response){
  137. var videos = response.responseXML.getElementsByTagName('Video');
  138. var file = null;
  139. var id = null;
  140. for (var i = 0; i < videos.length; i++) {
  141. var vparts = videos[i].getElementsByTagName('Part');
  142. if (vparts.length > 0) {
  143. file = vparts[0].attributes['file'].value;
  144. id = vparts[0].attributes['id'].value;
  145. if (videos[i].attributes['lastViewedAt'] === null || videos[i].attributes['lastViewedAt'] === undefined) {
  146. break;
  147. }
  148. }
  149. }
  150.  
  151. if (file !== null) {
  152. openItemOnAgent(file, id, openFolder).catch(function(){
  153. showToast('Failed to connect to agent, is it running or firewalled?',1);
  154. });
  155. }
  156. }).catch(function(){
  157. showToast('Failed to get information for directory',1);
  158. });
  159. }
  160. }
  161. }, function(error){
  162. showToast('Error getting metadata from' + metaDataPath, 1);
  163. });
  164. }
  165. };
  166.  
  167. var bindClicks = function() {
  168. jQuery(".glyphicon.play").each(function(i, e) {
  169. e = jQuery(e);
  170. if (!e.hasClass('plexextplayer')) {
  171. if (!e.parent().hasClass('hidden')) {
  172. e.addClass('plexextplayer');
  173. var parent = e.parent().parent();
  174. if (parent.is('li')) {
  175. 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>');
  176. parent.after(template);
  177. template.click(clickListener);
  178. } else if (parent.is('div') && parent.hasClass('media-poster-actions')) {
  179. var template = jQuery('<button class="play-btn media-poster-btn btn-link" tabindex="-1"><i class="glyphicon play plexextplayer plexextplayerico"></i></button>');
  180. parent.prepend(template);
  181. template.click(clickListener);
  182. }
  183. }
  184. }
  185. });
  186. };
  187.  
  188. // Make buttons smaller
  189. jQuery('body').append('<style>.media-poster-btn { padding: 8px !important; } .glyphicon.plexfolderextplayerico:before { content: "\\e343"; } .glyphicon.plexextplayerico:before { content: "\\e161"; }</style>');
  190.  
  191. // Bind buttons and check for new ones every 100ms
  192. setInterval(bindClicks, 100);
  193. bindClicks();