Skip to content

Instantly share code, notes, and snippets.

@tcha-tcho
Created March 10, 2025 17:39
Show Gist options
  • Save tcha-tcho/ca62b2733baded0da037e65c4cbc5a2a to your computer and use it in GitHub Desktop.
Save tcha-tcho/ca62b2733baded0da037e65c4cbc5a2a to your computer and use it in GitHub Desktop.
Intercept html calls for SEO and Card formation....
const isBot = require('./../helpers/isBot.js').isBot
const dummyPage = [
'<!doctype html> <html lang="en"> <head>'
,'</head> <body>'
,'</body> </html>'
]
const defTitle = 'Chats to follow';
const defLogo = 'https://unnon.com/ogimg.png';
const app_id = "267524633679818"
const fullDummyPage = (doc, req) => {
if (typeof doc !== "object") doc = {};
let d = doc;
let title = d.originalName||d.name||defTitle
let space = d.space ? global.id(d.space) : "";
let uid = d.uid ? global.id(d.uid) : "";
let desc = (d.about || space || uid || "Try Unnon!");
let image = d.media||defLogo;
image = image + "?v="+(d.mediaVersion||(new Date().getTime()))
let og = `<meta property="fb:app_id" content="${app_id}" />`;
og += `<meta property="og:type" content="website" />`;
og += `<meta property="og:title" content="${title}" />`;
og += `<meta property="og:description" content="${desc}" />`;
og += `<meta property="og:image" content="${image}" />`;
og += `<meta property="og:url" content="${req.url}" />`;
let tempPage = `<h1>${title}</h1><h3>${desc}</h3><img src='${image}'>`
return dummyPage[0]+og+dummyPage[1]+tempPage+dummyPage[2];
};
exports.fun = global.functions.https.onRequest((req, res) => {
res.set('Access-Control-Allow-Origin', "*")
res.set('Access-Control-Allow-Methods', 'GET, POST')
var path = req.path ? req.path.split('/') : req.path;
var host = req.get('host');
var origin = req.get('origin');
// if (host.indexOf("unn.one") !== -1 || origin.indexOf("unn.one") !== -1) {
// res.status(404).send('Not found');
// } else {
const sendNormalPage = function(){
res.send(global.indexHTML); // skip {data: ...}
};
const id = path[1];
const whitelist = [
"/login"
,"/logout"
,"/settings"
,"/direct/"
,"/starred"
,"/store"
,"/i/"
,"/invitation/"
,"/u/"
,"/about/"
,"/build/"
,"/css/"
,"/img/"
,"/imgLanding/"
,"/investors/"
,"/js/"
,"/serverFiles/"
]
if (id && isBot(req, whitelist) && (path && path.length > 1)) {
let collection = id.indexOf("-") === -1 ? "users" : "spaces";
global.admin.firestore().collection(collection).doc(id).get()
.then(function(snapObj){
res.send(fullDummyPage(snapObj.data(), req)); // skip data
})
.catch(function(err){
console.error(err)
sendNormalPage();
});
} else {
sendNormalPage();
}
// };
});
var url = require('url')
var crawlerUserAgents = [
'googlebot',
'Yahoo! Slurp',
'bingbot',
'yandex',
'baiduspider',
'facebookexternalhit',
'twitterbot',
'rogerbot',
'linkedinbot',
'embedly',
'quora link preview',
'showyoubot',
'outbrain',
'pinterest/0.',
'developers.google.com/+/web/snippet',
'slackbot',
'vkShare',
'W3C_Validator',
'redditbot',
'Applebot',
'WhatsApp',
'flipboard',
'tumblr',
'bitlybot',
'SkypeUriPreview',
'nuzzel',
'Discordbot',
'Google Page Speed',
'Qwantify',
'pinterestbot',
'Bitrix link preview',
'XING-contenttabreceiver',
'Chrome-Lighthouse',
'yahoou',
'yeti',
'yodaobot',
'gigabot',
'ia_archiver',
'opengraph.io',
'developers\.google\.com'
];
crawlerUserAgents = crawlerUserAgents.map(function(crawler){
return crawler.toLowerCase();
})
var extensionsToIgnore = [
'.js',
'.css',
'.xml',
'.less',
'.png',
'.jpg',
'.jpeg',
'.gif',
'.pdf',
'.doc',
'.txt',
'.ico',
'.rss',
'.zip',
'.mp3',
'.rar',
'.exe',
'.wmv',
'.doc',
'.avi',
'.ppt',
'.mpg',
'.mpeg',
'.tif',
'.wav',
'.mov',
'.psd',
'.ai',
'.xls',
'.mp4',
'.m4a',
'.swf',
'.dat',
'.dmg',
'.iso',
'.flv',
'.m4v',
'.torrent',
'.woff',
'.ttf',
'.svg',
'.webmanifest'
];
extensionsToIgnore = crawlerUserAgents.map(function(extension){
return extension.toLowerCase();
})
exports.isBot = function(req, whitelist, blacklist) {
var userAgent = req.headers['user-agent'].toLowerCase()
, bufferAgent = req.headers['x-bufferbot']
, isBotRequest = false
, path = (req.path||"").toLowerCase();
if (path) path = path.split('/')
if(!userAgent) return false;
if(req.method != 'GET' && req.method != 'HEAD') return false;
if(req.headers && req.headers['x-prerender']) return false;
//if it contains _escaped_fragment_, show prepared page
var parsedQuery = url.parse(req.url, true).query;
if(parsedQuery && parsedQuery['_escaped_fragment_'] !== undefined) {
isBotRequest = true;
}
//if it is a bot...show prepared page
if(crawlerUserAgents.some(function(crawlerUserAgent){
return userAgent.indexOf(crawlerUserAgent) !== -1;
})) isBotRequest = true;
//if it is BufferBot...show prepared page
if(bufferAgent) isBotRequest = true;
//if it is a bot and is requesting a resource...dont prepared
if(extensionsToIgnore.some(function(extension){
return req.url.indexOf(extension) !== -1;
})) return false;
//if it is a bot and not requesting a resource and is not whitelisted... dont prepared
if(Array.isArray(whitelist) && whitelist.some(function(whitelisted){
return (new RegExp(whitelisted)).test(path) === true;
})) return false;
//if it is a bot and not requesting a resource and is not blacklisted(url or referer)...dont prepared
if(Array.isArray(blacklist) && blacklist.some(function(blacklisted){
var blacklistedUrl = false
, blacklistedReferer = false
, regex = new RegExp(blacklisted);
blacklistedUrl = regex.test(path) === true;
if(req.headers['referer']) blacklistedReferer = regex.test(req.headers['referer']) === true;
return blacklistedUrl || blacklistedReferer;
})) return false;
return isBotRequest;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment