nativeshell.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. "filedownload",
  14. "remotevideo",
  15. "displaymode",
  16. "screensaver"
  17. ];
  18. const plugins = [
  19. 'mpvVideoPlayer',
  20. 'mpvAudioPlayer',
  21. 'jmpInputPlugin'
  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 "desktop";
  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. for (const option of setting.options) {
  191. safeValues[String(option.value)] = option.value;
  192. const opt = document.createElement("option");
  193. opt.value = option.value;
  194. opt.selected = option.value == values[setting.key];
  195. let optionName = option.title;
  196. const swTest = `${section.key}.${setting.key}.`;
  197. const swTest2 = `${section.key}.`;
  198. if (optionName.startsWith(swTest)) {
  199. optionName = optionName.substring(swTest.length);
  200. } else if (optionName.startsWith(swTest2)) {
  201. optionName = optionName.substring(swTest2.length);
  202. }
  203. opt.appendChild(document.createTextNode(optionName));
  204. control.appendChild(opt);
  205. }
  206. control.addEventListener("change", async (e) => {
  207. await new Promise(resolve => {
  208. window.api.settings.setValue(section.key, setting.key, safeValues[e.target.value], resolve);
  209. });
  210. if (setting.key == "devicetype") {
  211. section = (await new Promise(resolve => {
  212. window.api.settings.settingDescriptions(resolve);
  213. })).filter(x => x.key == section.key)[0];
  214. createSection(true);
  215. }
  216. });
  217. label.appendChild(document.createTextNode(setting.key + " "));
  218. label.appendChild(control);
  219. } else {
  220. const control = document.createElement("input");
  221. control.type = "checkbox";
  222. control.checked = values[setting.key];
  223. control.addEventListener("change", e => {
  224. window.api.settings.setValue(section.key, setting.key, e.target.checked);
  225. });
  226. label.appendChild(control);
  227. label.appendChild(document.createTextNode(" " + setting.key));
  228. }
  229. group.appendChild(label);
  230. }
  231. };
  232. createSection();
  233. }
  234. const closeContainer = document.createElement("div");
  235. Object.assign(closeContainer.style, {
  236. width: "100%",
  237. display: "flex",
  238. justifyContent: "flex-end",
  239. paddingTop: "10px"
  240. });
  241. modal.appendChild(closeContainer);
  242. const close = document.createElement("button");
  243. close.textContent = "Close"
  244. close.addEventListener("click", () => {
  245. modalContainer.remove();
  246. });
  247. closeContainer.appendChild(close);
  248. }