mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 16:05:44 -07:00
kaios: Add hooks for the KaiAds API
The Kai Store makes display of advertisements provided by the KaiAds API mandatory. I don't want such adverts to be inconvenient for the users, so I've just gone for adding a menu item that will display one. This is probably a little too crude, but it's good for testing things. The actual KaiAds API code is not free software, so it's not included here. My intention is to add it by hand to the Zip files for Kai Store uploads. Without it, the advertising code does nothing.
This commit is contained in:
@ -122,6 +122,8 @@ function(build_platform_extras)
|
|||||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${name}-manifest.webapp
|
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${name}-manifest.webapp
|
||||||
RENAME manifest.webapp
|
RENAME manifest.webapp
|
||||||
DESTINATION kaios/${name})
|
DESTINATION kaios/${name})
|
||||||
|
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/kaios/kaiads-glue.js
|
||||||
|
DESTINATION kaios/${name})
|
||||||
if (HALIBUT)
|
if (HALIBUT)
|
||||||
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/help
|
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/help
|
||||||
DESTINATION kaios/${name})
|
DESTINATION kaios/${name})
|
||||||
|
@ -13,7 +13,10 @@ print <<EOF;
|
|||||||
<meta http-equiv="Content-Type" content="text/html; charset=ASCII" />
|
<meta http-equiv="Content-Type" content="text/html; charset=ASCII" />
|
||||||
<meta name="theme-color" content="rgb(50,50,50)" />
|
<meta name="theme-color" content="rgb(50,50,50)" />
|
||||||
<title>${displayname}</title>
|
<title>${displayname}</title>
|
||||||
|
<!-- The KaiAds scripts are only present in Kai Store versions. -->
|
||||||
|
<script src="kaiads.v5.min.js"></script>
|
||||||
<script defer type="text/javascript" src="${name}.js"></script>
|
<script defer type="text/javascript" src="${name}.js"></script>
|
||||||
|
<script defer type="text/javascript" src="kaiads-glue.js"></script>
|
||||||
<!-- Override some defaults for small screens -->
|
<!-- Override some defaults for small screens -->
|
||||||
<script id="environment" type="application/json">
|
<script id="environment" type="application/json">
|
||||||
{ "PATTERN_DEFAULT": "10x10",
|
{ "PATTERN_DEFAULT": "10x10",
|
||||||
|
58
kaios/kaiads-glue.js
Normal file
58
kaios/kaiads-glue.js
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// Glue connecting Puzzles to the KaiAds API.
|
||||||
|
//
|
||||||
|
// The Kai Store requires that we support advertisements through
|
||||||
|
// KaiAds. To avoid polluting the Puzzles core with this, the
|
||||||
|
// relevant code is largely confined to this file. It can then be
|
||||||
|
// included in builds that are destined for the Kai Store and left out
|
||||||
|
// of others. The main puzzle code, and the KaiAds API (as supplied
|
||||||
|
// by Kai Technologies) should be loaded before this file.
|
||||||
|
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
// To run, we need to be on KaiOS with the KaiAds SDK and the
|
||||||
|
// Open Web Apps API.
|
||||||
|
if (!getKaiAd || !navigator.mozApps ||
|
||||||
|
navigator.userAgent.toLowerCase().indexOf('kaios') == -1) return;
|
||||||
|
// If those prerequisites are satisfied, install the button.
|
||||||
|
var advertbutton = document.createElement("button");
|
||||||
|
advertbutton.type = "button";
|
||||||
|
advertbutton.textContent = "Display an advert...";
|
||||||
|
advertbutton.disabled = true;
|
||||||
|
var advertli = document.createElement("li");
|
||||||
|
advertli.appendChild(advertbutton);
|
||||||
|
menuform.querySelector("ul").appendChild(advertli);
|
||||||
|
// Now work out whether we're installed from the Store (and hence
|
||||||
|
// want real adverts) or not (and hence want test ones).
|
||||||
|
var selfrequest = navigator.mozApps.getSelf();
|
||||||
|
selfrequest.onerror = function() {
|
||||||
|
console.log("Error getting own app record: ", selfrequest.error.name);
|
||||||
|
// Leave the button disabled.
|
||||||
|
};
|
||||||
|
selfrequest.onsuccess = function() {
|
||||||
|
var testmode = selfrequest.result.installOrigin !=
|
||||||
|
"app://kaios-plus.kaiostech.com";
|
||||||
|
|
||||||
|
advertbutton.addEventListener("click", function(e) {
|
||||||
|
// The KaiAds SDK provides this function.
|
||||||
|
getKaiAd({
|
||||||
|
publisher: 'dac9c115-ec42-4175-ac5e-47e118cc541b',
|
||||||
|
test: testmode ? 1 : 0,
|
||||||
|
timeout: 5000,
|
||||||
|
onready: function(ad) {
|
||||||
|
ad.on('close', function () {
|
||||||
|
// KaiAds adds inline styles to the body and doesn't
|
||||||
|
// remove them, so we do it ourselves.
|
||||||
|
document.body.style = '';
|
||||||
|
onscreen_canvas.focus();
|
||||||
|
});
|
||||||
|
ad.call('display');
|
||||||
|
},
|
||||||
|
onerror: function(err) {
|
||||||
|
alert(`Sorry; no advert available (KaiAds error ${err}).`);
|
||||||
|
onscreen_canvas.focus(); // Close the menu.
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
advertbutton.disabled = false;
|
||||||
|
};
|
||||||
|
})();
|
@ -56,5 +56,15 @@ print encode_json({
|
|||||||
categories => ["games"],
|
categories => ["games"],
|
||||||
type => "web",
|
type => "web",
|
||||||
cursor => JSON::PP::false,
|
cursor => JSON::PP::false,
|
||||||
|
# These permissions could be removed on builds without KaiAds,
|
||||||
|
# but that's a bit complicated.
|
||||||
|
permissions => {
|
||||||
|
mobiledata => {
|
||||||
|
description => "Required to display advertisements"
|
||||||
|
},
|
||||||
|
wifidata => {
|
||||||
|
description => "Required to display advertisements"
|
||||||
|
},
|
||||||
|
},
|
||||||
$decvers ? (version => $decvers) : (),
|
$decvers ? (version => $decvers) : (),
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user