Google Drive - Default Text in Search Field

Pre-populate Google Drive search field with some custom text (default: "* type:folder") for easier searching.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==

// @name                Google Drive - Default Text in Search Field
// @description         Pre-populate Google Drive search field with some custom text (default: "* type:folder") for easier searching.
// @version             1.1

// @namespace           io.github.ni554n
// @match               https://drive.google.com/*
// @run-at              document-idle

// @supportURL          https://github.com/ni554n/userscripts/issues
// @license             MIT

// @author              Nissan Ahmed
// @homepageURL         https://ni554n.github.io/
// @contributionURL     https://paypal.me/ni554n

// ==/UserScript==

// Change this text to customize the default text.
const INPUT_PLACEHOLDER = "* type:folder";

const isLogEnabled = false;

const searchInputElement = document.querySelector(`input[aria-label="Search in Drive"]`);
const titleElement = document.head.getElementsByTagName("title")[0];

// Don't know why but any userscript on Google Drive executes twice.
// First time normally but on the second call, every getElementsBy*() returns null.
if (searchInputElement) {
  // Pre-fill the input for the first load.
  searchInputElement.value ||= INPUT_PLACEHOLDER;

  // Google Drive is an SPA where page navigation is done dynamically via javascript.
  // Page navigations can be detected by observing the <title> changes.
  new MutationObserver(observePageNavigation).observe(
    titleElement,
    { childList: true },
  );
}

function observePageNavigation() {
  log(`Page Route Changed -> ${titleElement.text}`);

  searchInputElement.value ||= INPUT_PLACEHOLDER;
}

function log(message) {
  if (isLogEnabled) console.log(message);
}