nativeshell.js 8.4 KB

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