Skip to content

Instantly share code, notes, and snippets.

@noromanba
Last active October 31, 2016 23:39
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/3705f2e9ebb8538328b2f3f9a6f1a9b3 to your computer and use it in GitHub Desktop.
Save noromanba/3705f2e9ebb8538328b2f3f9a6f1a9b3 to your computer and use it in GitHub Desktop.
canonicalize URL for UserScript/Bookmarklet
// ==UserScript==
// @name canonicalizer
// @namespace http://noromanba.flavors.me
// @description canonicalize URL for UserScript/Bookmarklet
// @include http://example.com/DIY
// @grant none
// @noframes
// @run-at document-body
// @version 2016.10.31.1
// @homepage https://gist.github.com/noromanba/3705f2e9ebb8538328b2f3f9a6f1a9b3
// @downloadURL https://gist.github.com/noromanba/3705f2e9ebb8538328b2f3f9a6f1a9b3/raw/canonicalizer.user.js
// @contributor noromanba https://gist.github.com/noromanba/d730ccf3ae5e6916cd60 (Fork of)
// @license MIT License https://nrm.mit-license.org/2016
// @author noromanba http://noromanba.flavors.me
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/BSicon_uxSPLa.svg/128px-BSicon_uxSPLa.svg.png
// ==/UserScript==
// Icon (PD by YLSS)
// https://commons.wikimedia.org/wiki/File%3ABSicon_uxSPLa.svg
// Devel
// https://gist.github.com/noromanba/3705f2e9ebb8538328b2f3f9a6f1a9b3
//*/
(() => {
const canonical = () => {
'use strict';
// avoid contextual `head.querySelector`, nullable; e.g. PDFium
// og:url c.f.
// http://ogp.me
return (document.querySelector('head meta[property="og:url"][content]') || {}).content ||
(document.querySelector('head link[rel="canonical"][href]') || {}).href ||
location.href;
};
//location.href = canonical();
// TODO replaceState keep title in browser history
//history.replaceState(null, null, canonical());
history.pushState(null, null, canonical());
})();
// Bookmarklet
/*/
javascript:(()=>{const canonical=()=>{return(document.querySelector('head meta[property="og:url"][content]')||{}).content||(document.querySelector('head link[rel="canonical"][href]')||{}).href||location.href;};history.pushState(null,null,canonical());})();
//*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment