Skip to content

Commit

Permalink
Implement NativeShell
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylyzo committed Feb 1, 2020
1 parent faddf9a commit 215263d
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 6 deletions.
33 changes: 32 additions & 1 deletion org.jellyfin.webos/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ var server_info = false;
var manifest = false;
var textToInject = false;

var appInfo = {
deviceId: null,
deviceName: 'LG Smart TV',
appName: 'Jellyfin for WebOS',
appVersion: '0.0.0'
};

//Adds .includes to string to do substring matching
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
Expand Down Expand Up @@ -107,6 +114,11 @@ function handleCheckbox(elem, evt) {
return false;
}

// Similar to jellyfin-web
function generateDeviceId() {
return btoa([navigator.userAgent, new Date().getTime()].join('|')).replace(/=/g, '1');
}

function navigationInit() {
if (isVisible(document.querySelector('#connect'))) {
document.querySelector('#connect').focus()
Expand All @@ -116,7 +128,22 @@ function navigationInit() {
}

function Init() {
appInfo.deviceId = storage.get('device_id');
if (!appInfo.deviceId) {
appInfo.deviceId = generateDeviceId();
storage.set('device_id', appInfo.deviceId);
}

webOS.fetchAppInfo(function (info) {
if (info) {
appInfo.appVersion = info.version;
} else {
console.error('Error occurs while getting appinfo.json.');
}
});

navigationInit();

if (storage.exists('connected_server')) {
data = storage.get('connected_server')
document.querySelector('#baseurl').value = data.baseurl
Expand Down Expand Up @@ -329,6 +356,7 @@ function handoff(url) {
contentFrame.contentDocument.removeEventListener('DOMContentLoaded', onLoad);
contentFrame.removeEventListener('load', onLoad);

injectScriptText(contentFrame.contentDocument, 'window.AppInfo = ' + JSON.stringify(appInfo) + ';');
injectScriptText(contentFrame.contentDocument, textToInject);
}

Expand Down Expand Up @@ -367,11 +395,14 @@ window.addEventListener('message', function (msg) {
var contentFrame = document.querySelector('#contentFrame');

switch (msg.type) {
case 'AppHost.exit':
case 'selectServer':
document.querySelector('.container').style.display = '';
hideConnecting();
contentFrame.style.display = 'none';
contentFrame.src = '';
break;
case 'AppHost.exit':
webOS.platformBack();
break;
}
});
114 changes: 109 additions & 5 deletions org.jellyfin.webos/js/webOS.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function() {
(function(AppInfo) {
'use strict';

console.log('WebOS adapter');
Expand All @@ -10,9 +10,113 @@
}, '*');
}

window.webOS = {
platformBack: function () {
postMessage('AppHost.exit');
// List of supported features
var SupportedFeatures = [
'exit',
'externallinkdisplay',
'htmlaudioautoplay',
'htmlvideoautoplay',
'imageanalysis',
'physicalvolumecontrol',
'displaylanguage',
'otherapppromotions',
'targetblank',
'screensaver',
'subtitleappearancesettings',
'subtitleburnsettings',
'chromecast',
'multiserver'
];

window.NativeShell = {
AppHost: {
init: function () {
postMessage('AppHost.init', AppInfo);
return Promise.resolve(AppInfo);
},

appName: function () {
postMessage('AppHost.appName', AppInfo.appName);
return AppInfo.appName;
},

appVersion: function () {
postMessage('AppHost.appVersion', AppInfo.appVersion);
return AppInfo.appVersion;
},
deviceId: function () {
postMessage('AppHost.deviceId', AppInfo.deviceId);
return AppInfo.deviceId;
},

deviceName: function () {
postMessage('AppHost.deviceName', AppInfo.deviceName);
return AppInfo.deviceName;
},

exit: function () {
postMessage('AppHost.exit');
},

getDefaultLayout: function () {
postMessage('AppHost.getDefaultLayout', 'tv');
return 'tv';
},

getDeviceProfile: function (profileBuilder) {
postMessage('AppHost.getDeviceProfile');
return profileBuilder({ enableMkvProgressive: false });
},

getSyncProfile: function (profileBuilder) {
postMessage('AppHost.getSyncProfile');
return profileBuilder({ enableMkvProgressive: false });
},

supports: function (command) {
var isSupported = command && SupportedFeatures.indexOf(command.toLowerCase()) != -1;
postMessage('AppHost.supports', {
command: command,
isSupported: isSupported
});
return isSupported;
}
},

selectServer: function () {
postMessage('selectServer');
},

downloadFile: function(url) {
postMessage('downloadFile', { url: url });
},

enableFullscreen: function() {
postMessage('enableFullscreen');
},

disableFullscreen: function() {
postMessage('disableFullscreen');
},

getPlugins: function() {
postMessage('getPlugins');
return [];
},

openUrl: function(url, target) {
postMessage('openUrl', {
url: url,
target: target
});
},

updateMediaSession: function(mediaInfo) {
postMessage('updateMediaSession', { mediaInfo: mediaInfo });
},

hideMediaSession: function() {
postMessage('hideMediaSession');
}
};
})();
})(window.AppInfo);

0 comments on commit 215263d

Please sign in to comment.