Highlight file types

Highlight file type labels in Google search results

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Highlight file types
// @name:ja         ファイルの種類を強調表示
// @namespace       https://greasyfork.org/users/783910
// @version         0.5.2
// @description     Highlight file type labels in Google search results
// @description:ja  Google検索結果でファイルの種類ラベルを強調表示する
// @author          ysnr777
// @match           https://www.google.com/search?*
// @grant           none
// @license         WTFPL
// @run-at          document-end
// ==/UserScript==

'use strict';

const fileSettings = {
  PDF: {
    title: 'Adobe Portable Document Format (.pdf)',
    background: '#FDE3E4',
    color: '#EC1C24',
    iconSlug: 'adobeacrobatreader',
  },
  XLS: {
    title: 'Microsoft Excel (.xls, .xlsx)',
    background: '#E9F9F0',
    color: '#217346',
    iconSlug: 'microsoftexcel',
  },
  DOC: {
    title: 'Microsoft Word (.doc, .docx)',
    background: '#E1EAF7',
    color: '#2B579A',
    iconSlug: 'microsoftword',
  },
  PPT: {
    title: 'Microsoft PowerPoint (.ppt, .pptx)',
    background: '#F9EAE7',
    color: '#B7472A',
    iconSlug: 'microsoftpowerpoint',
  },
  KML: {
    title: 'Google Earth (.kml, .kmz)',
    background: '#E9F1FE',
    color: '#4285F4',
    iconSlug: 'googleearth',
  },
  default: {
    background: '#FFFF99',
    color: '#4D5156',
  },
};

const targetQuery = 'span.NaCKVc';

/**
 * 強調表示を実行する
 * @param {HTMLSpanElement[]} targets
 */
const highlighting = targets => {
  for (const el of targets) {
    const setting = fileSettings[el.textContent in fileSettings ? el.textContent : 'default'];

    // style
    el.style.fontWeight = 'bold';
    el.style.backgroundColor = setting.background;
    el.style.borderColor = setting.color;
    el.style.color = setting.color;

    // title
    if ('title' in setting) {
      el.title = setting.title;
    }

    // icon
    if ('iconSlug' in setting) {
      const span = document.createElement('span');
      const img = span.appendChild(document.createElement('img'));

      // get image from CDN
      img.src = `https://cdn.simpleicons.org/${setting.iconSlug}/${setting.color.slice(1)}`;

      // style
      span.style.marginRight = '0.5em';
      img.style.height = '1em';

      el.prepend(span);
    }
  }
};

// ページ読み込み時に実行
highlighting(document.querySelectorAll(targetQuery));

// 連続スクロールで増えた要素に実行
const observer = new MutationObserver((mutations, observer) => {
  for (const record of mutations) {
    for (const node of record.addedNodes) {
      highlighting(node.querySelectorAll(targetQuery));
    }
  }
});
observer.observe(
  document.getElementById('center_col'),
  {
    childList: true,
    subtree: true
  }
);
window.addEventListener('beforeunload', () => {
  observer.disconnect();
});