Notion Navigation Fixer

Allow browser nav options like back forward pgup pgdown

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Notion Navigation Fixer
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Allow browser nav options like back forward pgup pgdown
// @author       Andreas Huttenrauch
// @match        *://www.notion.so/*
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    function uniqid() {
        return (new Date().getTime()).toString(16) + Math.random().toString(16).substring(2, 15);
    }

    function windowId() {
        var id = window.name;
        if ( id == "" ) {
            id = uniqid();
            window.name = id;
        }
        return id;
    }

    function getStack() {
        var stack = JSON.parse(GM_getValue("notion_stack_"+windowId(), "{}"));
        if ( typeof stack != "object" ) stack = {};
        if ( ! ("index" in stack) ) stack.index = 0;
        if ( ! ("data" in stack) ) stack.data = [];
        return stack;
    }

    function setStack(stack) {
        GM_setValue("notion_stack_"+windowId(), JSON.stringify(stack));
    }

    function pushLoc() {
        var loc = window.location.href;
        var stack = getStack();
        if ( stack.index < stack.data.length ) return; // don't push if we navigated back in history
        stack.data.push(loc);
        stack.data = stack.data.slice(-20);
        stack.index = stack.data.length;
        setStack(stack);
    }

    function goBack() {
        var stack = getStack();
        if ( stack.index < 1 ) return;
        stack.index--;
        var loc = stack.data[stack.index];
        setStack(stack);
        window.location.href = loc;
    }

    function goForward() {
        var stack = getStack();
        if ( stack.index >= stack.data.length ) return;
        stack.index++;
        var loc = stack.data[stack.index];
        setStack(stack);
        window.location.href = loc;
    }

/*
    window.addEventListener('popstate', function(e) {
        if ( trapInit && trapCaught ) {
            e.preventDefault();
            e.stopImmediatePropagation();
        }
        trapCaught = true;
    });
*/

    document.addEventListener('mousedown', function(e) {
        if ( (e.buttons & 8) == 8 ) {
            e.stopImmediatePropagation();
            e.preventDefault();
            goBack();
        }
        if ( (e.buttons & 16) == 16 ) {
            e.stopImmediatePropagation();
            e.preventDefault();
            goForward();
        }
    });

    document.addEventListener('keydown', function(e) {
        //console.log(e);
        /*
    if ( e.target.contentEditable ) {
        console.log("not moving because you are editing something important");
        return;
    }
    */
        if ( e.keyCode == 37 && e.altKey ) {
            goBack();
        }
        if ( e.keyCode == 39 && e.altKey ) {
            goForward();
        }
        var mainDiv = document.querySelector(".notion-frame .notion-scroller");
        if ( mainDiv == "undefined" ) return;
        var scrollAmt = parseInt(window.innerHeight*0.8);
        if ( e.keyCode == 34 && e.shiftKey == false && e.ctrlKey == false ) {
            e.stopImmediatePropagation();
            e.preventDefault();
            mainDiv.scrollBy(0, scrollAmt);
        }
        if ( e.keyCode == 33 && e.shiftKey == false && e.ctrlKey == false ) {
            e.stopImmediatePropagation();
            e.preventDefault();
            mainDiv.scrollBy(0, -scrollAmt);
        }
    });


    pushLoc();

})();