notion-enhancer/extension/repo/bypass-preview@cb6fd684-f113-4a7a-9423-8f0f0cff069f/client.mjs

42 lines
1.2 KiB
JavaScript

/*
* notion-enhancer core: bypass-preview
* (c) 2021 dragonwocky <thedragonring.bod@gmail.com> (https://dragonwocky.me/)
* (https://notion-enhancer.github.io/) under the MIT license
*/
'use strict';
import { web } from '../../api/_.mjs';
web.whenReady().then(async () => {
const openAsPage = document.querySelector(
'.notion-peek-renderer [style*="height: 45px;"] a'
);
if (openAsPage) openAsPage.click();
});
function getCurrentPage() {
const previewID = location.search
.slice(1)
.split('&')
.map((opt) => opt.split('='))
.find((opt) => opt[0] === 'p');
if (previewID) return { type: 'preview', id: previewID[1] };
return { type: 'page', id: location.pathname.split(/(-|\/)/g).reverse()[0] };
}
let lastPage = getCurrentPage();
web.addDocumentObserver((event) => {
const currentPage = getCurrentPage();
if (currentPage.id !== lastPage.id || currentPage.type !== lastPage.type) {
const openAsPage = document.querySelector(
'.notion-peek-renderer [style*="height: 45px;"] a'
);
if (openAsPage) {
if (currentPage.id === lastPage.id && currentPage.type === 'preview') {
history.back();
} else openAsPage.click();
}
lastPage = getCurrentPage();
}
});