nativeshell.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. const viewdata = JSON.parse(window.atob("@@data@@"));
  2. const features = [
  3. "filedownload",
  4. "displaylanguage",
  5. "htmlaudioautoplay",
  6. "htmlvideoautoplay",
  7. "externallinks",
  8. "clientsettings",
  9. "multiserver",
  10. "remotecontrol",
  11. "fullscreenchange",
  12. "filedownload",
  13. "remotevideo",
  14. "displaymode",
  15. "screensaver"
  16. ];
  17. const plugins = [
  18. 'mpvVideoPlayer',
  19. 'mpvAudioPlayer',
  20. 'jmpInputPlugin',
  21. 'jmpUpdatePlugin'
  22. ];
  23. function loadScript(src) {
  24. return new Promise((resolve, reject) => {
  25. const s = document.createElement('script');
  26. s.src = src;
  27. s.onload = resolve;
  28. s.onerror = reject;
  29. document.head.appendChild(s);
  30. });
  31. }
  32. // Add plugin loaders
  33. for (const plugin of plugins) {
  34. window[plugin] = async () => {
  35. await loadScript(`${viewdata.scriptPath}${plugin}.js`);
  36. return window["_" + plugin];
  37. };
  38. }
  39. window.NativeShell = {
  40. openUrl(url, target) {
  41. window.api.system.openExternalUrl(url);
  42. },
  43. downloadFile(downloadInfo) {
  44. window.api.system.openExternalUrl(downloadInfo.url);
  45. },
  46. openClientSettings() {
  47. showSettingsModal();
  48. },
  49. getPlugins() {
  50. return plugins;
  51. }
  52. };
  53. function getDeviceProfile() {
  54. return {
  55. 'Name': 'Jellyfin Media Player',
  56. 'MusicStreamingTranscodingBitrate': 1280000,
  57. 'TimelineOffsetSeconds': 5,
  58. 'TranscodingProfiles': [
  59. {'Type': 'Audio'},
  60. {
  61. 'Container': 'ts',
  62. 'Type': 'Video',
  63. 'Protocol': 'hls',
  64. 'AudioCodec': 'aac,mp3,ac3,opus,flac,vorbis',
  65. 'VideoCodec': 'h264,h265,hevc,mpeg4,mpeg2video',
  66. 'MaxAudioChannels': '6'
  67. },
  68. {'Container': 'jpeg', 'Type': 'Photo'}
  69. ],
  70. 'DirectPlayProfiles': [{'Type': 'Video'}, {'Type': 'Audio'}, {'Type': 'Photo'}],
  71. 'ResponseProfiles': [],
  72. 'ContainerProfiles': [],
  73. 'CodecProfiles': [],
  74. 'SubtitleProfiles': [
  75. {'Format': 'srt', 'Method': 'External'},
  76. {'Format': 'srt', 'Method': 'Embed'},
  77. {'Format': 'ass', 'Method': 'External'},
  78. {'Format': 'ass', 'Method': 'Embed'},
  79. {'Format': 'sub', 'Method': 'Embed'},
  80. {'Format': 'sub', 'Method': 'External'},
  81. {'Format': 'ssa', 'Method': 'Embed'},
  82. {'Format': 'ssa', 'Method': 'External'},
  83. {'Format': 'smi', 'Method': 'Embed'},
  84. {'Format': 'smi', 'Method': 'External'},
  85. {'Format': 'pgssub', 'Method': 'Embed'},
  86. {'Format': 'dvdsub', 'Method': 'Embed'},
  87. {'Format': 'pgs', 'Method': 'Embed'}
  88. ]
  89. };
  90. }
  91. async function createApi() {
  92. await loadScript('qrc:///qtwebchannel/qwebchannel.js');
  93. const channel = await new Promise((resolve) => {
  94. /*global QWebChannel */
  95. new QWebChannel(window.qt.webChannelTransport, resolve);
  96. });
  97. return channel.objects;
  98. }
  99. window.NativeShell.AppHost = {
  100. init() {
  101. window.apiPromise = createApi();
  102. (async () => {
  103. window.api = await window.apiPromise;
  104. })();
  105. },
  106. getDefaultLayout() {
  107. return viewdata.mode;
  108. },
  109. supports(command) {
  110. return features.includes(command.toLowerCase());
  111. },
  112. getDeviceProfile,
  113. getSyncProfile: getDeviceProfile,
  114. appName() {
  115. return "Jellyfin Media Player";
  116. },
  117. appVersion() {
  118. return navigator.userAgent.split(" ")[1];
  119. },
  120. deviceName() {
  121. return viewdata.deviceName;
  122. }
  123. };
  124. async function showSettingsModal() {
  125. let settings = await new Promise(resolve => {
  126. window.api.settings.settingDescriptions(resolve);
  127. });
  128. const modalContainer = document.createElement("div");
  129. Object.assign(modalContainer.style, {
  130. position: "fixed",
  131. top: 10,
  132. bottom: 10,
  133. left: 10,
  134. right: 10,
  135. zIndex: 2000,
  136. width: "100%",
  137. height: "100%",
  138. backgroundColor: "#000000C0",
  139. display: "flex",
  140. justifyContent: "center"
  141. });
  142. modalContainer.addEventListener("click", e => {
  143. if (e.target == modalContainer) {
  144. modalContainer.remove();
  145. }
  146. });
  147. document.body.appendChild(modalContainer);
  148. const modalContainer2 = document.createElement("div");
  149. Object.assign(modalContainer2.style, {
  150. width: "100%",
  151. maxWidth: "500px",
  152. overflowY: "auto",
  153. margin: "20px",
  154. height: "auto"
  155. });
  156. modalContainer.appendChild(modalContainer2);
  157. const modal = document.createElement("div");
  158. modal.className = "jmp-settings-modal";
  159. Object.assign(modal.style, {
  160. width: "100%",
  161. padding: "20px",
  162. boxSizing: "border-box",
  163. backgroundColor: "#202020",
  164. height: "min-content",
  165. color: "#fff"
  166. });
  167. modalContainer2.appendChild(modal);
  168. const title = document.createElement("h1");
  169. title.textContent = "Jellyfin Media Player Settings";
  170. modal.appendChild(title);
  171. for (let section of settings) {
  172. const group = document.createElement("fieldset");
  173. modal.appendChild(group);
  174. const createSection = async (clear) => {
  175. if (clear) {
  176. group.innerHTML = "";
  177. }
  178. const values = await new Promise(resolve => {
  179. window.api.settings.allValues(section.key, resolve);
  180. });
  181. const legend = document.createElement("legend");
  182. legend.textContent = section.key;
  183. group.appendChild(legend);
  184. for (const setting of section.settings) {
  185. const label = document.createElement("label");
  186. label.style.display = "block";
  187. if (setting.options) {
  188. const safeValues = {};
  189. const control = document.createElement("select");
  190. control.style.maxWidth = "350px";
  191. for (const option of setting.options) {
  192. safeValues[String(option.value)] = option.value;
  193. const opt = document.createElement("option");
  194. opt.value = option.value;
  195. opt.selected = option.value == values[setting.key];
  196. let optionName = option.title;
  197. const swTest = `${section.key}.${setting.key}.`;
  198. const swTest2 = `${section.key}.`;
  199. if (optionName.startsWith(swTest)) {
  200. optionName = optionName.substring(swTest.length);
  201. } else if (optionName.startsWith(swTest2)) {
  202. optionName = optionName.substring(swTest2.length);
  203. }
  204. opt.appendChild(document.createTextNode(optionName));
  205. control.appendChild(opt);
  206. }
  207. control.addEventListener("change", async (e) => {
  208. await new Promise(resolve => {
  209. window.api.settings.setValue(section.key, setting.key, safeValues[e.target.value], resolve);
  210. });
  211. if (setting.key == "devicetype") {
  212. section = (await new Promise(resolve => {
  213. window.api.settings.settingDescriptions(resolve);
  214. })).filter(x => x.key == section.key)[0];
  215. createSection(true);
  216. }
  217. });
  218. label.appendChild(document.createTextNode(setting.key + " "));
  219. label.appendChild(control);
  220. } else {
  221. const control = document.createElement("input");
  222. control.type = "checkbox";
  223. control.checked = values[setting.key];
  224. control.addEventListener("change", e => {
  225. window.api.settings.setValue(section.key, setting.key, e.target.checked);
  226. });
  227. label.appendChild(control);
  228. label.appendChild(document.createTextNode(" " + setting.key));
  229. }
  230. group.appendChild(label);
  231. }
  232. };
  233. createSection();
  234. }
  235. const closeContainer = document.createElement("div");
  236. Object.assign(closeContainer.style, {
  237. width: "100%",
  238. display: "flex",
  239. justifyContent: "flex-end",
  240. paddingTop: "10px"
  241. });
  242. modal.appendChild(closeContainer);
  243. const close = document.createElement("button");
  244. close.textContent = "Close"
  245. close.addEventListener("click", () => {
  246. modalContainer.remove();
  247. });
  248. closeContainer.appendChild(close);
  249. }