Created
December 27, 2018 13:20
-
-
Save muhammadfaizan/0ab8409e34a542a0040b2b79e2bb40f1 to your computer and use it in GitHub Desktop.
Gist to redirect app based on link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
(function() { | |
var appPath = '{{{appPath}}}'; | |
var pageType = { | |
CART: '/cart/', | |
PRODUCT: '/d/', | |
CATALOG: '/c/', | |
WISHLIST: '/w/', | |
LOGIN: '/l/', | |
REGISTER: '/r/', | |
SEARCH: '/s/' | |
}; | |
{{!/* creates an object named "device" */}} | |
{{>components/scripts/device-detection-js}} | |
function findPageType(target, el) { | |
var result = { | |
type: false, | |
path: null | |
}; | |
if (el) { | |
var escapeUrls = [ | |
'#', | |
'?source=footer', | |
'/catalog/filters', | |
'?sort=', | |
'?form=', | |
'checkout/multistep', | |
'customer/order', | |
'customer/reviewsratings/index', | |
'customer/newsletter/manage', | |
'customer/account/logout', | |
'catalog/productspecifications/sku' | |
]; | |
escapeUrls = escapeUrls.concat({{{escapeUrls}}}); | |
var href = el.getAttribute('href'); | |
if (escapeUrls.some(path => href.indexOf(path) > -1)) { | |
return false; | |
} | |
if (el.getAttribute('data-emit-event') === 'wishlist.add') { | |
return false; | |
} | |
var sku = el.getAttribute('data-sku'); | |
href = href.replace(/\/\w\w\//, '/'); | |
if (sku) { | |
result.type = pageType.PRODUCT; | |
result.path = pageType.PRODUCT + sku; | |
} else if ((href.indexOf('/customer/account/login') > 0) || | |
(href.indexOf('/dialog/oauth?') > 0)) { | |
result.type = pageType.LOGIN; | |
result.path = pageType.LOGIN; | |
} else if (href.indexOf('/customer/account/create') > 0) { | |
result.type = pageType.REGISTER; | |
result.path = pageType.REGISTER; | |
} else if(href.indexOf('/customer/wishlist/index') > 0) { | |
result.type = pageType.WISHLIST; | |
result.path = pageType.WISHLIST; | |
} else if (href.indexOf('/cart') > 0 || | |
href.indexOf('/onepagecheckout') > 0 || | |
href.indexOf('/checkout/index') > 0) { | |
result.type = pageType.CART; | |
result.path = pageType.CART; | |
} else if (el.getAttribute('class') === 'dropdown--list--item') { | |
{{! //ignoring language change }} | |
return false; | |
} else { | |
var paths = href.split('/'); | |
if (paths.length === 5 && href.indexOf('.html') === -1) { | |
result.type = pageType.CATALOG; | |
result.path = pageType.CATALOG + paths[3]; | |
} else { | |
return false; | |
} | |
} | |
} else { | |
targetDiv = target.closest('div'); | |
if (targetDiv && | |
targetDiv.getAttribute('class') | |
.indexOf('ssg--products-cta--button btn btn-default btn-m js-suggester-products-cta') > -1) { | |
var inp = document.getElementsByTagName('input'); | |
result.type = pageType.SEARCH; | |
result.path = pageType.SEARCH + encodeURI(inp); | |
} else { | |
return false; | |
} | |
} | |
return result; | |
} | |
if (getMobileOperatingSystem() === device.ANDROID) { | |
document.querySelector('body').addEventListener('click', function(evt) { | |
var anchor = evt.target.closest('a'); | |
window.event = anchor; | |
if (anchor !== null) { | |
var segment = findPageType(evt.target, anchor); | |
if (segment) { | |
evt.preventDefault(); | |
var appRoute = appPath + segment.path; | |
window.location.assign(appRoute); | |
setTimeout(function() { | |
window.location.assign(anchor.href); | |
}, 0); | |
} | |
} else { | |
var segment = findPageType(evt.target); | |
if (segment) { | |
var appRoute = appPath + segment.path; | |
setTimeout(function() { | |
window.location.assign(appRoute); | |
}, 0); | |
} | |
} | |
}); | |
} | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment