Skip to content

Instantly share code, notes, and snippets.

@noromanba
Last active June 15, 2018 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noromanba/da0de2ed47a381ee8c1e to your computer and use it in GitHub Desktop.
Save noromanba/da0de2ed47a381ee8c1e to your computer and use it in GitHub Desktop.
normalize redundant canonical on www.amazon.tld for UserScript
// ==UserScript==
// @name Amaznorm
// @namespace http://noromanba.flavors.me
// @description normalize redundant url/canonical on www.amazon.tld for UserScript
// @include https://www.amazon.tld/*
// @include https://www.amazon.com/*
// @include https://www.amazon.com.au/*
// @include https://www.amazon.com.br/*
// @include https://www.amazon.com.mx/*
// @grant none
// @run-at document-body
// @noframes
// @version 2018.6.15.0
// @homepage https://gist.github.com/noromanba/da0de2ed47a381ee8c1e
// @downloadURL https://gist.github.com/noromanba/da0de2ed47a381ee8c1e/raw/amaznorm.user.js
// @license MIT License https://nrm.mit-license.org/2015
// @author noromanba https://noromanba.github.com
// @icon https://www.clker.com/cliparts/X/Q/2/3/6/f/man-pushing-back-th.png
// ==/UserScript==
// Icon (PD by JACKSON ECHOKA)
// http://www.clker.com/clipart-man-pushing-back.html
// Bookmarklet
// http://let.hatelabo.jp/noromanba/let/hJmcjrPV1ecx
// Devel
// https://gist.github.com/noromanba/da0de2ed47a381ee8c1e
// e.g.
// https://www.amazon.co.jp/%E5%87%8D%E7%B5%90%E9%98%B2%E6%AD%A2%E5%89%A4-%E5%A1%A9%E5%8C%96%E3%83%8A%E3%83%88%E3%83%AA%E3%82%A6%E3%83%A025kg%C3%9740%E8%A2%8B-%E5%90%88%E8%A8%881000kg-%E8%9E%8D%E9%9B%AA%E5%89%A4-%E5%86%B7%E5%87%8D%E9%96%A2%E4%BF%82%E3%81%AE%E3%83%96%E3%83%A9%E3%82%A4%E3%83%B3/dp/B018QKEPII
// |
// v
// https://www.amazon.co.jp/dp/B018QKEPII
(() => {
'use strict';
// ".com.xx" unsupported Magic TLD c.f.
// https://web.archive.org/https://wiki.greasespot.net/Magic_TLD
// https://publicsuffix.org/list/
// https://en.wikipedia.org/wiki/Amazon.com#Website
// TBD omit hostname assertion
if (!/(?:www\.)*amazon\.[a-z]{2,3}(?:\.[a-z]{2})*$/.test(location.hostname) ||
!['/dp/', '/gp/', '/ASIN/', ].find(s => location.pathname.includes(s))) {
return;
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
// -> 1st-exists | undefined
const canon = (document.head.querySelector([
'link[rel="canonical"][href]'
]) || {});
if (!canon || !canon.href) return;
// redundant canonical syntax;
// https://www.amazon.<TLD>/<URL_ENCODED_ITEM_NAME>/dp/<ASIN>
const norm = [
location.origin,
'/',
new URL(canon.href).pathname.split('/').slice(-2).join('/'),
].join('');
// avoid canonical mismatch
canon.href = norm;
//*
window.history.pushState(null, null, norm);
/*/
// TODO title blank in Browser History
window.history.replaceState(null, document.title, norm);
//*/
})();
// DEV
//
// https://www.amazon.co.jp/%E3%83%97%E3%83%AA%E3%83%B3%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0-%E9%AB%98%E3%81%95472cm%EF%BC%81%E3%83%96%E3%83%A9%E3%82%AD%E3%82%AA%E3%82%B5%E3%82%A6%E3%83%AB%E3%82%B9%E5%A4%A7%E5%9E%8B%E9%80%A0%E5%BD%A2%E7%89%A9%EF%BC%88%E6%81%90%E7%AB%9C%E7%AD%89%E8%BA%AB%E5%A4%A7%E3%83%95%E3%82%A3%E3%82%AE%E3%83%A5%E3%82%A2%EF%BC%89/dp/B00789E5H0
// https://www.amazon.co.jp/dp/B00789E5H0
// https://www.amazon.co.jp/One-Only-Jewellery-%E9%91%91%E5%AE%9A%E6%9B%B8%E4%BB%98%E3%80%91%E3%83%80%E3%82%A4%E3%83%A4%E3%83%A2%E3%83%B3%E3%83%89-31-04ct/dp/B07CJH1YP1
// https://www.amazon.co.jp/dp/B018QKEPII
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment