Overleaf - File History

Lets you use the browser history to navigate between previously opened files

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Overleaf - File History
// @namespace    https://github.com/BLumbye/overleaf-userscripts
// @version      0.1
// @description  Lets you use the browser history to navigate between previously opened files
// @author       Benjamin Lumbye
// @license      GPL-3
// @match        https://www.overleaf.com/project/*
// @grant        none
// ==/UserScript==

'use strict';

(function () {
  // Listen for file open events
  _ide.$scope.$on('doc:after-opened', onFileOpen);
  _ide.$scope.$on('file-view:file-opened', onFileOpen);

  // Listen for popstate event
  window.addEventListener('popstate', (event) => {
    if (event.state?.fileID) {
      openFile(event.state.fileID);
    }
  });
})();

function onFileOpen(event) {
  const fileID = _ide.fileTreeManager.selected_entity_id;
  if (history.state?.fileID === fileID) return;
  history.pushState({ fileID }, '');
}

function openFile(fileID) {
  const file = _ide.fileTreeManager.findEntityById(fileID);
  if (!file) return;
  if (file.type === 'file') {
    // BUG: File does not get highlighted in the file tree
    _ide.binaryFilesManager.openFile(file);
  } else if (file.type === 'doc') {
    _ide.editorManager.openDoc(file);
  }
}