Skip to content

Instantly share code, notes, and snippets.

@EllinaKuznetcova
Created July 5, 2023 15:29
Show Gist options
  • Save EllinaKuznetcova/0c7842e8c923d9194634c4ed1b44480a to your computer and use it in GitHub Desktop.
Save EllinaKuznetcova/0c7842e8c923d9194634c4ed1b44480a to your computer and use it in GitHub Desktop.
appBridge.js
const appBridge = {
initialize: function () {
if (window.location.host.indexOf('web-demo') !== -1)
return;
console.log('initialize appBridge');
appBridge.parseDom();
window.addEventListener("message", (event) => {
if (event.data === "luresRendered") {
console.log('Parsing DOM for appBridge (luresEnabled)');
appBridge.parseDom();
}
}, false);
},
parseDom: function () {
const links = document.querySelectorAll("a");
for (let i = 0; i < links.length; i++) {
if (links[i].pathname === '/account/login' ||
links[i].pathname === '/login') {
links[i].href = '';
links[i].onclick = function (event) {
appBridge.navigateTo('login');
return false;
};
}
if (links[i].pathname.startsWith('/search')) {
links[i].setAttribute('categoryId', links[i].href.split('?c=')[1]);
links[i].href = '';
links[i].onclick = function (event) {
appBridge.navigateTo('pageByPath', '/app-category/' + this.getAttribute('categoryId'));
return false;
};
}
if (links[i].pathname === '/account/register' ||
links[i].pathname === '/register') {
links[i].href = '';
links[i].onclick = function (event) {
appBridge.track('generate_lead', {
article_last_viewed: window.lureArticle.articleId
});
appBridge.navigateTo('register');
return false;
};
}
//changeconsent
if (links[i].href.indexOf('changeconsent') !== -1) {
links[i].href = '';
links[i].onclick = function (event) {
appBridge.navigateTo('cookieConsent');
return false;
};
}
//Internal links => _blank
if (links[i].hash.substring(0, 1) !== '#' &&
(links[i].host === window.location.host || links[i].host.indexOf("www." + pubbleEnv.domain) !== -1) &&
window.location.host !== "web-demo.pubble.dev") {
const split = links[i].pathname.split('/');
if (split.length > 3 && !isNaN(split[3])) {
links[i].href = '';
links[i].onclick = function (event) {
appBridge.navigateTo('article', split[3]);
return false;
};
} else {
links[i].target = "_blank";
links[i].href = 'https://www.' + pubbleEnv.domain + links[i].pathname;
}
}
//iOS sends users to another page when using hashes..
if (links[i].hash.substring(0, 1) === '#') {
links[i].href = '';
}
}
postMessage("domParsed");
},
//Will be deprecated -> selectTabById
selectTab: function (tab) {
console.log('AppBridge selectTab ' + tab);
/*
newsfeed
kiosk,
podcasts,
puzzles,
readinglist
*/
if (window.Android) {
Android.selectTab(tab);
}
if (window.webkit) {
window.webkit.messageHandlers.selectTab.postMessage(tab);
}
},
navigateTo: function (name, ...arguments) {
console.log('AppBridge navigateTo ' + name);
let obj = {};
switch (name) {
case 'article' :
obj = {
name: name,
articleId: parseInt(arguments[0]),
articleType: arguments[1] ?? 'InternetArticle'
}
break;
case 'pageByPath' :
obj = {
name: 'pageByPath',
path: arguments[0], // '/podcast/1234', /app-category/1234
title: arguments[1] ?? '',
};
break;
default:
obj = {
name: name // loginOrRegister / login / account / osAppSettings / cookieConsent
};
}
const json = JSON.stringify(obj);
if (window.Android) {
Android.navigateTo(json);
}
if (window.webkit) {
window.webkit.messageHandlers.navigateTo.postMessage(json);
}
},
track: function (name, params) {
let obj = {
name: name,
params: params
};
if (name === undefined) {
obj = {
name: 'articleOpen',
params: {
articleId: window.lureArticle.articleId,
userId: window.pubbleEnv.userId,
articlesLeft: window.lureArticle.articlesLeft,
articleAccessLevel: window.lureArticle.articleAccessLevel,
userAccessLevel: window.lureArticle.userAccessLevel
}
};
}
const json = JSON.stringify(obj);
if (window.Android) {
Android.track(json);
}
if (window.webkit) {
window.webkit.messageHandlers.track.postMessage(json);
}
},
setEnableVoiceOver: function () {
if (window.Android) {
Android.setEnableVoiceOver(true);
}
if (window.webkit) {
window.webkit.messageHandlers.setEnableVoiceOver.postMessage(true);
}
},
startGallery: function (startAtIndex, gallery) {
console.log('startGallery ' + startAtIndex + gallery);
const galleryNodes = document.querySelectorAll('[data-gallery]');
const imageNodes = [];
galleryNodes.forEach((node) => {
// Check if the gallery parameter is provided and if the node's data-gallery value matches
if (gallery && node.getAttribute('data-gallery') !== gallery) {
return; // Skip this node if it doesn't match the gallery parameter
}
const index = node.getAttribute('data-index');
const caption = node.getAttribute('data-caption');
const credit = node.getAttribute('data-credit');
const imageUrl = node.getAttribute('data-src');
imageNodes.push({
index: index,
caption: caption,
credit: credit,
imageUrl: imageUrl
});
});
const json = JSON.stringify({
startAtIndex: startAtIndex,
images: imageNodes
});
console.log(json);
if (window.Android) {
Android.openGallery(json);
}
if (window.webkit) {
window.webkit.messageHandlers.openGallery.postMessage(json);
}
}
};
appBridge.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment