Skip to content

Instantly share code, notes, and snippets.

@mooz
Last active February 16, 2019 02:50
Show Gist options
  • Save mooz/0f60fa2d0b502f3d70b325f7cf4625b8 to your computer and use it in GitHub Desktop.
Save mooz/0f60fa2d0b502f3d70b325f7cf4625b8 to your computer and use it in GitHub Desktop.
(function (history) {
// 表示する履歴の数
const maxHistory = 5;
if ($('#history-box').length) { return; }
const $header = $(".quick-launch");
$header.append($(`<div class='flex-box'>
<div class='flex-item'>
<div class='left-box' id='history-box' />
</div></div>`));
const $historyHolder = $("#history-box");
function pathToPageTitle(path) {
return decodeURIComponent(path.split("/")[2]);
}
let pushState = history.pushState;
history.pushState = function (state) {
try {
// on history change
var pageTitle = pathToPageTitle(state.path);
if (pageTitle) {
let $newHistory = $(`<span>→ <a href='${state.path}'>${pageTitle}</a></span>`);
$newHistory.on("click", () => { $newHistory.remove(); });
$historyHolder.append($newHistory);
if ($historyHolder.children().length > maxHistory) {
$historyHolder.children().first().remove();
}
}
} catch (x) {
console.error(x);
}
return pushState.apply(history, arguments);
};
})(window.history);
#history-box span {
margin: 0 3px;
}
#history-box span:last-child {
font-weight: bold;
pointer-events: none;
cursor: default;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment