Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created May 3, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/f3f905a85105c10aa6cfa566f7dc4268 to your computer and use it in GitHub Desktop.
Save ZiTAL/f3f905a85105c10aa6cfa566f7dc4268 to your computer and use it in GitHub Desktop.
hbbtv: cookie and localstorage tester
<!DOCTYPE html PUBLIC "-//HbbTV//1.1.1//EN" "http://www.hbbtv.org/dtd/HbbTV-1.1.1.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="content-type" content="application/vnd.hbbtv.xhtml+xml;charset=utf-8"/>
</head>
<body>
<div class="cookie">
<h1>Cookie:</h1>
<p>none</p>
</div>
<div class="localstorage">
<h1>LocalStorage:</h1>
<p>none</p>
</div>
<div style="display: flex" onclick="setItems(); window.location.href = window.location.href;">
<div style="background-color: red; width: 10px; height: 10px;"></div><span>set Items</span>
</div>
<div style="display: flex" onclick="removeItems(); window.location.href = window.location.href;">
<div style="background-color: green; width: 10px; height: 10px;"></div><span>Remove Items</span>
</div>
<!--
<div style="display: flex">
<div style="background-color: yellow; width: 10px; height: 10px;"></div><span>Reload page</span>
</div>
-->
<div style="display: flex">
<div style="background-color: blue; width: 10px; height: 10px;"></div><span>Reload page</span>
</div>
<div style="display: none; width: 0px; height: 0px; position: absolute; left: 0px; top: 0px;">
<!--
ApplicationManager extracts information out of the AIT (such as lifecycle, autostart, etc.)
-->
<object id="appMan" type="application/oipfApplicationManager">
</object>
<object id="appmgr" type="application/oipfApplicationManager">
</object>
<object id="oipfcfg" type="application/oipfConfiguration">
</object>
</div>
<script>
//<![CDATA[
if (typeof(KeyEvent)!='undefined') {
if (typeof(KeyEvent.VK_LEFT)!='undefined') {
var VK_LEFT = KeyEvent.VK_LEFT;
var VK_UP = KeyEvent.VK_UP;
var VK_RIGHT = KeyEvent.VK_RIGHT;
var VK_DOWN = KeyEvent.VK_DOWN;
}
if (typeof(KeyEvent.VK_ENTER)!='undefined') {
var VK_ENTER = KeyEvent.VK_ENTER;
}
if (typeof(KeyEvent.VK_RED)!='undefined') {
var VK_RED = KeyEvent.VK_RED;
var VK_GREEN = KeyEvent.VK_GREEN;
var VK_YELLOW = KeyEvent.VK_YELLOW;
var VK_BLUE = KeyEvent.VK_BLUE;
}
if (typeof(KeyEvent.VK_PLAY)!='undefined') {
var VK_PLAY = KeyEvent.VK_PLAY;
var VK_PAUSE = KeyEvent.VK_PAUSE;
var VK_STOP = KeyEvent.VK_STOP;
var VK_PLAY_PAUSE = KeyEvent.VK_PLAY_PAUSE;
}
if (typeof(KeyEvent.VK_FAST_FWD)!='undefined') {
var VK_FAST_FWD = KeyEvent.VK_FAST_FWD;
var VK_REWIND = KeyEvent.VK_REWIND;
}
if (typeof(KeyEvent.VK_BACK)!='undefined') {
var VK_BACK = KeyEvent.VK_BACK;
}
if (typeof(KeyEvent.VK_0)!='undefined') {
var VK_0 = KeyEvent.VK_0;
var VK_1 = KeyEvent.VK_1;
var VK_2 = KeyEvent.VK_2;
var VK_3 = KeyEvent.VK_3;
var VK_4 = KeyEvent.VK_4;
var VK_5 = KeyEvent.VK_5;
var VK_6 = KeyEvent.VK_6;
var VK_7 = KeyEvent.VK_7;
var VK_8 = KeyEvent.VK_8;
var VK_9 = KeyEvent.VK_9;
}
}
if (typeof(VK_LEFT)=='undefined') {
var VK_LEFT = 0x25;
var VK_UP = 0x26;
var VK_RIGHT = 0x27;
var VK_DOWN = 0x28;
}
if (typeof(VK_ENTER)=='undefined') {
var VK_ENTER = 0x0d;
}
if (typeof(VK_RED)=='undefined') {
var VK_RED = 0x74;
var VK_GREEN = 0x75;
var VK_YELLOW = 0x76;
var VK_BLUE = 0x77;
}
if (typeof(VK_PLAY)=='undefined') {
var VK_PLAY = 0x50;
var VK_PAUSE = 0x51;
var VK_STOP = 0x53;
var VK_PLAY_PAUSE = 0;
}
if (typeof(VK_FAST_FWD)=='undefined') {
var VK_FAST_FWD = 0x46;
var VK_REWIND = 0x52;
}
if (typeof(VK_BACK)=='undefined') {
var VK_BACK = 0xa6;
}
if (typeof(VK_0)=='undefined') {
var VK_0 = 0x30;
var VK_1 = 0x31;
var VK_2 = 0x32;
var VK_3 = 0x33;
var VK_4 = 0x34;
var VK_5 = 0x35;
var VK_6 = 0x36;
var VK_7 = 0x37;
var VK_8 = 0x38;
var VK_9 = 0x39;
}
//]]>
</script>
<script>
//<![CDATA[
/**
* HbbTV library v1.0
*
* (C) 2009, IRT GmbH
*
* Overview:
*
* You need to add the OIPF application manager and the oipf configuration
* embedded object to your HTML DOM tree e.g.:
*
<div style="visibility: hidden; display: none;">
<object type="application/oipfApplicationManager" id="oipfAppMan"> </object>
<object type="application/oipfConfiguration" id="oipfConfig"> </object>
</div>
*
* Before using any other function call the initializer first.
*
* function hbbtvlib_initialize()
* Creates and initializes HbbTV, i.e. OIPF DAE, embedded objects.
* Always call this function prior to other functions of this library.
*
* function hbbtvlib_show()
* Shows the application and requests keys via the keyset object.
*
* function hbbtvlib_hide()
* Hides the application and releases keys via the keyset object.
*
* function hbbtvlib_createApp()
* Create applications with the OIPF Application Manager
*
* function hbbtvlib_closeApp()
* Destroys this application.
*
* function hbbtvlib_init_broadcast()
* Convenience function that integrates the broadcast video in your application.
*
* function hbbtvlib_release_broadcast()
* Convenience function that removes the broadcast video from your application.
*
* function hbbtvlib_current_service()
* Returns the DVB service ID.
*/
/**
* The last error occured in hbbtvlib. If one of the functions returns false,
* the error can be retrieved via this property.
*/
var hbbtvlib_lastError = "no error";
/**
* @return true if OIPF functions are available
*/
function hbbtvlib_initialize() {
try {
int_createOipfObjs();
var appMgr = int_objs[int_objTypes.appMan];
if (appMgr && typeof(appMgr.getOwnerApplication) != "undefined")
int_app = appMgr.getOwnerApplication(document);
else {
hbbtvlib_lastError = "no application manager";
int_app = null;
return false;
}
return int_initKeysetObj();
} catch (e) {
hbbtvlib_lastError = e;
int_app = null;
}
return false;
};
/**
* This function does everything HbbTV requires an application to do
* when it wants to get visible.
*
* @return
*/
function hbbtvlib_show() {
try {
int_app.show();
if (typeof int_app.activate != "undefined") int_app.activate();
if (typeof int_app.activateInput != "undefined") int_app.activateInput();
int_keyset.setValue(int_ksVisible);
return true;
} catch (e) {
hbbtvlib_lastError = e;
return false;
}
}
/**
* This function does everything HbbTV requires an application to do
* when it wants to transition to the hidden state.
*
* @return
*/
function hbbtvlib_hide() {
try {
int_app.hide();
int_keyset.setValue(int_ksHidden);
return true;
} catch (e) {
hbbtvlib_lastError = e;
return false;
}
}
/**
* Overrides the default keysets.
* Default is RED for hidden state, all for visible state.
*
* @param visibleSet a keyset bitmask
* @param hiddenSet a keyset bitmask
* @return
*/
function hbbtvlib_setKeysets(visibleSet, hiddenSet) {
if (typeof visibleSet == "Number") int_ksVisible = visibleSet;
if (typeof hiddenSet == "Number") int_ksHidden = hiddenSet;
};
/**
* Starts a new application and destroys this application.
*
* @param dvbUrl including organization and application ids
* @param httpUrl a fallback URL
*
* @return false
*/
function hbbtvlib_createApp(dvbUrl, httpUrl) {
if (int_app) {
try {
if (dvbUrl && int_app.createApplication(dvbUrl, false)) {
int_app.destroyApplication();
return true;
}
} catch (e) {
hbbtvlib_lastError = e;
}
try {
if (httpUrl && int_app.createApplication(httpUrl, false)) {
int_app.destroyApplication();
return true;
}
} catch (e) {
hbbtvlib_lastError = e;
}
} else {
document.location.href = httpUrl;
}
return false;
};
/**
* Destroys this application.
*
* @param fallBackUrl
* @return
*/
function hbbtvlib_closeApp(fallBackUrl) {
if (int_app) {
try {
int_app.destroyApplication();
} catch (e) {
hbbtvlib_lastError = e;
}
} else if (fallBackUrl) {
document.location.href = fallBackUrl;
} else {
window.close();
}
return false;
};
/**
* Handle OIPF DAE Broadcast video
*/
var int_bc_video = null;
var int_bc_container = null;
/**
* Creates and initializes a broadcast video inside the element
* identified by the containerId. If no bc video can be included
* the dummy picture is added instead of the bc.
*
* @param parentId the id of the HTML container where the video/broadcast object will be added
* @param objId id which shall be set for the video/broadcast object
* @param dummyPic an optional picture to be shown if video/broadcast can not be added
*
* @return the video/broadcast object or false
*/
function hbbtvlib_init_broadcast (parentId, objId, dummyPic) {
try {
int_bc_container = document.getElementById(parentId);
if (!int_bc_container) return false;
// root container for video/broadcast object
int_bc_container.innerHTML = '<object id="' + objId + '" type="video/broadcast"> </object>';
int_bc_video = document.getElementById(objId);
if (typeof (int_bc_video.fullScreen) != 'undefined') {
int_bc_video.setFullScreen(false);
}
if (typeof (int_bc_video.bindToCurrentChannel) != 'undefined') {
int_bc_video.bindToCurrentChannel();
}
// check if OIPF video object is supported
if (typeof (int_bc_video.currentChannel) != 'undefined') return int_bc_video;
} catch (e) {
hbbtvlib_lastError = e;
}
if (dummyPic) int_bc_container.innerHTML = '<img id="'+ objId + '" src="' + dummyPic + '" alt="TV Bild" />';
return false;
};
/**
* Releases the video/broadcast object and removes the object from the DOM.
*/
function hbbtvlib_release_broadcast () {
try {
if (int_bc_video && typeof (int_bc_video.release) != "undefined") {
int_bc_video.release();
int_bc_video = null;
}
if (int_bc_container) {
int_bc_container.innerHTML = "";
int_bc_container = null;
}
} catch (e) {
hbbtvlib_lastError = e;
}
};
/**
* Requires hbbtvlib_init_broadcast
*
* @return the DVB service id of the current presented service or -1
*/
function hbbtvlib_current_service () {
try {
return int_bc_video.currentChannel.sid;
} catch (e) {
hbbtvlib_lastError = e;
}
return -1;
};
/**
* Tunes to a DVB service identified by the DVB Triplet. The application may
* get killed due to the application life cycle, i.e. if it is not signalled
* with their application ID on the tuned service.
*
* @param onid the original network id
* @param tsid the transport stream id
* @param sid the service id
*
* @return false if there is no video broadcast object available or there is no
* channel found for this triplet
*/
function hbbtvlib_tuneTo(onid, tsid, sid) {
try {
var chLst = int_bc_video.getChannelConfig().channelList;
onid = (typeof(onid) == 'number') ? onid : parseInt(onid, 10);
tsid = (typeof(tsid) == 'number') ? tsid : parseInt(tsid, 10);
sid = (typeof(sid) == 'number') ? sid : parseInt(sid, 10);
var ch = chLst.getChannelByTriplet(onid, tsid, sid);
if (ch == null) {
hbbtvlib_lastError = "Tuning failed, no channel object found for given DVB triplet.";
return false;
}
int_bc_video.setChannel(ch, false);
return true;
}catch (e) {
hbbtvlib_lastError = "Tuning failed due to " + e;
}
return false;
};
/*
* Library internal (private) properties and functions.
*/
/**
* Array of OIPF DAE embedded objects. Access Key is the mime-type without "application/".
*/
var int_objs = new Array();
var int_objTypes = {
appMan: "oipfApplicationManager",
config: "oipfConfiguration"
};
/**
* OIPF DAE Application object
* - to show/hide the application
* - create or destroy applications
* - providing the keyset object
*/
var int_app = null;
/**
* The OIPF keyset object used to request keys from terminal.
*
* Usually this is the RED button in hidden mode, and up to all
* available keys in visible mode.
*/
var int_keyset = null;
var int_ksHidden = null;
var int_ksVisible = null;
/**
* Internal function to create OIPF embedded objects,
* like ApplicationManager etc., if not already existing.
*/
function int_createOipfObjs() {
if (int_objs.length) return;
// Look for existing objects
var objects = document.getElementsByTagName("object");
for (var i=0; i<objects.length; i++) {
var sType = objects.item(i).getAttribute("type");
//alert(sType);
for (var eType in int_objTypes) {
if (sType == "application/" + int_objTypes[eType]) {
int_objs[int_objTypes[eType]] = objects.item(i);
}
}
}
// create missing objects
var oipfObjs = document.createElement("div");
var objCreated = false;
for (var typeId in int_objTypes) {
var type = int_objTypes[typeId];
if (!int_objs[type]) {
var obj = document.createElement("object");
obj.setAttribute("type", "application/"+type);
oipfObjs.appendChild(obj);
int_objs[type] = obj;
objCreated = true;
}
}
if (objCreated) {
oipfObjs.setAttribute("style", "visibility: hidden; width: 0; height: 0;");
document.getElementsByTagName("body")[0].appendChild(oipfObjs);
}
};
/**
* Retrieves the keyset object for HbbTV 1.1.1 and 0.5 implementations.
*
* Sets default keysets for hidden and visible modes.
*/
function int_initKeysetObj () {
// try HbbTV 1.1.1
try {
int_keyset = int_app.privateData.keyset;
hbbtvlib_lastError = "privateData";
} catch (e) {
int_keyset = null;
// try HbbTV 0.5
try {
var tmp = int_objs[int_objTypes.config].keyset;
tmp.setValue = function (val) {this.value = val;};
int_keyset = tmp;
hbbtvlib_lastError = "oipfConfig";
} catch (e) {
int_keyset = null;
}
}
if (int_keyset) {
// set default key sets
if (!int_ksHidden) int_ksHidden = int_keyset.RED;
if (!int_ksVisible) int_ksVisible = 0x33F; // color + nav + vcr + numeric + alpha
}
return int_keyset != null;
};
//]]>
</script>
<script>
//<![CDATA[
//Keyset Launcher
function _setKeyset(mask)
{
// HbbTV 0.5:
try
{
var elemcfg = document.getElementById('oipfcfg');
elemcfg.keyset.value = mask;
}
catch (e)
{
// ignore
}
// HbbTV 1.0:
try
{
var elemcfg = document.getElementById('oipfcfg');
elemcfg.keyset.setValue(mask);
}
catch (e)
{
// ignore
}
// HbbTV 1.1.1:
try
{
var app = document.getElementById('appmgr').getOwnerApplication(document);
app.privateData.keyset.setValue(mask);
app.privateData.keyset.value = mask;
}
catch (e)
{
// ignore
}
}
//]]>
</script>
<script>
//<![CDATA[
(function()
{
var cookieStorage =
{
getItem: function (sKey)
{
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure)
{
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey))
return false;
var sExpires = "";
if (vEnd)
{
switch (vEnd.constructor)
{
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
return true;
},
removeItem: function (sKey, sPath, sDomain)
{
if (!sKey || !this.hasItem(sKey))
return false;
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function (sKey)
{
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: /* optional method: you can safely remove it! */ function ()
{
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var nIdx = 0; nIdx < aKeys.length; nIdx++)
aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
return aKeys;
}
};
var empty = function(item)
{
while(item.hasChildNodes())
item.removeChild(item.firstChild);
};
var r = function()
{
// teklak
document.onkeydown = function(e)
{
if(navigator.userAgent.match(/PHILIPS_OLS/)||navigator.userAgent.match(/NETTV/)||navigator.userAgent.match(/PhilipsWebKit/))
{
//
}
else
var e = window.event || e;
e.preventDefault();
switch (e.keyCode)
{
case VK_RED:
case 403:
setItems();
window.location.href = window.location.href;
break;
case VK_GREEN:
case 404:
removeItems();
window.location.href = window.location.href;
break;
case VK_BLUE:
case 406:
window.location.href = window.location.href;
break;
}
};
function hasItems()
{
var p = document.querySelector('.cookie > p');
var txt = '';
if(cookieStorage.hasItem('example'))
{
empty(p);
var v = cookieStorage.getItem('example');
txt = "cookie value = "+v;
}
p.appendChild(document.createTextNode(txt));
var p = document.querySelector('.localstorage > p');
var txt = '';
var v = localStorage.getItem('example');
if(v!==null)
{
empty(p);
txt = "localStorage value = "+v;
}
p.appendChild(document.createTextNode(txt));
}
function setItems()
{
localStorage.setItem('example', 'example');
cookieStorage.setItem('example', 'example');
}
function removeItems()
{
cookieStorage.removeItem('example');
localStorage.removeItem('example');
localStorage.clear();
}
window.hasItems = hasItems;
window.setItems = setItems;
window.removeItems = removeItems;
hasItems();
};
hbbtvlib_initialize();
_setKeyset(0x1 + 0x2 + 0x4 + 0x8 + 0x10 + 0x20 +0x100);
r();
})();
//]]>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment