Highlight file types

Highlight file type labels in Google search results

当前为 2023-09-21 提交的版本,查看 最新版本

// ==UserScript==
// @name            Highlight file types
// @name:ja         ファイルの種類を強調表示
// @namespace       https://gf.qytechs.cn/users/783910
// @version         0.4.0
// @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',
  },
};

for (const el of document.querySelectorAll('span.NaCKVc')) {
  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);
  }
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址