Skip to content

Instantly share code, notes, and snippets.

@montmanu
Last active February 7, 2017 00:00
Show Gist options
  • Save montmanu/b61d5076e7082b789740bf7faa3de10d to your computer and use it in GitHub Desktop.
Save montmanu/b61d5076e7082b789740bf7faa3de10d to your computer and use it in GitHub Desktop.
bookmarklet to load a hybrid version of current article
javascript:!function(a,b){function c(){let b="production";const c=a.location.hostname,d=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;return(/local/.test(c)||d.test(c))&&(b="local"),/dev/.test(c)&&(b="development"),/stg/.test(c)&&(b="staging"),b}function d(){let a="article";const c=b.querySelector('meta[name="PT"]');return c&&c.getAttribute("content")&&/blog/i.test(c.getAttribute("content"))&&(a="blogpost"),a}function e(){let a="";const c=b.querySelector('meta[name="articleid"]');return c&&c.getAttribute("content")&&/\d{13,19}/.test(c.getAttribute("content"))&&(a=c.getAttribute("content")),a}function f(){const a=d(),b=e(),c=`/hybrid/${a}/${b}.html`;return c}function g(){const a={local:"local.sbx.nytimes.com",development:"mobile.dev.nytimes.com",staging:"mobile.stg.nytimes.com",production:"mobile.nytimes.com"},b=c();return a[b]}function h(){const a={local:"http:",development:"https:",staging:"https:",production:"https:"},b=c();return a[b]}function i(){const a=f(),b=g(),c=h();return`${c}//${b}${a}`}function j(){const b=i();a.open(b,"_blank")}j()}(window,document);
(function (w, d){
function getEnv() {
let env = 'production';
const currentHostname = w.location.hostname;
const ipAddressPattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if (/local/.test(currentHostname) || ipAddressPattern.test(currentHostname)) {
env = 'local';
}
if (/dev/.test(currentHostname)) {
env = 'development';
}
if (/stg/.test(currentHostname)) {
env = 'staging';
}
return env;
}
function getAssetType() {
let assetType = 'article';
const pageTypeMeta = d.querySelector('meta[name="PT"]');
if (
pageTypeMeta &&
pageTypeMeta.getAttribute('content') &&
/blog/i.test(pageTypeMeta.getAttribute('content'))
) {
assetType = 'blogpost';
}
return assetType;
}
function getAssetId() {
let assetId = '';
const articleIdMeta = d.querySelector('meta[name="articleid"]');
if (
articleIdMeta &&
articleIdMeta.getAttribute('content') &&
/\d{13,19}/.test(articleIdMeta.getAttribute('content'))
) {
assetId = articleIdMeta.getAttribute('content');
}
return assetId;
}
function getHybridPath() {
const assetType = getAssetType();
const assetId = getAssetId();
const hybridPath = `/hybrid/${assetType}/${assetId}.html`;
return hybridPath;
}
function getHybridHostname() {
const hybridHostnames = {
local: 'local.sbx.nytimes.com',
development: 'mobile.dev.nytimes.com',
staging: 'mobile.stg.nytimes.com',
production: 'mobile.nytimes.com',
};
const env = getEnv();
return hybridHostnames[env];
}
function getHybridProtocol() {
const hybridProtocols = {
local: 'http:',
development: 'https:',
staging: 'https:',
production: 'https:',
};
const env = getEnv();
return hybridProtocols[env];
}
function getHybridUrl() {
const hybridPath = getHybridPath();
const hybridHostname = getHybridHostname();
const hybridProtocol = getHybridProtocol();
return `${hybridProtocol}//${hybridHostname}${hybridPath}`;
}
function openHybridUrl() {
const hybridUrl = getHybridUrl();
w.open(hybridUrl, '_blank');
}
openHybridUrl();
})(window, document);
!function(a,b){function c(){let b="production";const c=a.location.hostname,d=/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;return(/local/.test(c)||d.test(c))&&(b="local"),/dev/.test(c)&&(b="development"),/stg/.test(c)&&(b="staging"),b}function d(){let a="article";const c=b.querySelector('meta[name="PT"]');return c&&c.getAttribute("content")&&/blog/i.test(c.getAttribute("content"))&&(a="blogpost"),a}function e(){let a="";const c=b.querySelector('meta[name="articleid"]');return c&&c.getAttribute("content")&&/\d{13,19}/.test(c.getAttribute("content"))&&(a=c.getAttribute("content")),a}function f(){const a=d(),b=e(),c=`/hybrid/${a}/${b}.html`;return c}function g(){const a={local:"local.sbx.nytimes.com",development:"mobile.dev.nytimes.com",staging:"mobile.stg.nytimes.com",production:"mobile.nytimes.com"},b=c();return a[b]}function h(){const a={local:"http:",development:"https:",staging:"https:",production:"https:"},b=c();return a[b]}function i(){const a=f(),b=g(),c=h();return`${c}//${b}${a}`}function j(){const b=i();a.open(b,"_blank")}j()}(window,document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment