Image Alt to Title

Hover tooltip of image displaying alt attribute, original title and URL info.

目前為 2021-01-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Image Alt to Title
// @namespace   myfonj
// @include     *
// @grant       none
// @version     1.1.1
// @run-at      document-start
// @description Hover tooltip of image displaying alt attribute, original title and URL info.
// ==/UserScript==
/*
 * https://greasyfork.org/en/scripts/418348/versions/new
 * 
 * § Trivia:
 * ¶ Hover tooltip displays content of nearest element's title attribute (@title).
 * ¶ Alt attribute (@alt) is possible only at IMG element.
 * ¶ IMG@alt is not displayed in tooltip.
 * ¶ IMG cannot have children.
 * ¶ @title is possible on any element, including IMG.
 * ¶ IMG@src is also valuable.
 * 
 * Goal:
 * Display image alt attribute value in images hover tooltip, add valuable @SRC chunks.
 * 
 * Details
 * Pull @alt from image and set it so it is readable as @title tooltip
 * so that produced title value will not obscure existing parent title
 * that would be displayed otherwise.  Also include image filename from @src,
 * and additionally path or domain.
 * 
 * Means
 * Load (and error?) event listener constructing and setting title.
 * 
 * Dangers
 * Artificially altered alt or title after image load event will not be taken into account.
 * Mitigate with mutationObserver?
 * 
 * Process
 * Draw the rest of the owl
 * 
 * 
 * § Tastcases
 * 
 * FROM:
 * <a>
 *  <img>
 * </a>
 * TO:
 * <a>
 *  <img title="Alt missing.">
 * </a> 
 * 
 * FROM:
 * <a>
 *  <img alt="">
 * </a>
 * TO:
 * <a>
 *  <img alt="" title="Alt: ''">
 * </a> 
 * 
 * FROM:
 * <a>
 *  <img alt="░">
 * </a>
 * TO:
 * <a>
 *  <img alt="░" title="Alt: ░">
 * </a> 
 * 
 * FROM:
 * <a>
 *  <img alt="░" title="▒">
 * </a>
 * TO:
 * <a>
 *  <img title="Alt: ░, title: ▒">
 * </a> 

 * FROM:
 * <a title="▒">
 *  <img alt="░">
 * </a>
 * TO:
 * <a>
 *  <img title="Alt: ░, title: ▒">
 * </a> 
 * 
 * */

document.documentElement.addEventListener('load', altPic, true);
// document.documentElement.addEventListener('error', altPic, true);

/**
 * @param {{target: HTMLImageElement}} event
 */
function altPic (event) {
  const separator = '\n---\n';
  try {
    const img = event.target;
    if (img.tagName != 'IMG') return

    var desc = '';

    const alt = img.getAttribute('alt');
    const title = getClosestTitle(img);

    switch (alt) {
      case null:
        desc = 'Alt completely missing!';
        break;
      case '':
        desc = 'Alt: \'\'';
        break;
      default:
        if( alt == title ) {
          desc = 'Alt (=title): ';
        } else {
          desc = 'Alt: ';
        }
        desc += alt;
    }

    if (title && alt != title) {
      desc += separator;
      desc += 'Title: ' + title;
    }

    const descby = img.getAttribute('described-by');
    if (descby) {
      desc += separator;
      desc += 'Described by: ' + descby;
    }

    desc += separator;

    const srcURI = new URL(img.src, img.baseURI);
    const slugRx = /[^/]+$/;
    if (['https:', 'http:'].includes(srcURI.protocol)) {
      desc += 'File: ' + srcURI.pathname.match(slugRx);
      if (srcURI.search) {
        desc += '\nParams: ' + srcURI.search;
      }
      if (document.location.hostname != srcURI.hostname) {
        desc += '\nHost: ' + srcURI.hostname;
      }
      desc += '\nPath: ' + srcURI.pathname.replace(slugRx, '');
    } else {
      desc += 'Src: ' + img.src;
    }

    img.title = desc
  } catch (e) {
    // console.error(e);
  }
}

/**
 * @param {HTMLElement} el
 */
function getClosestTitle (el) {
  var ret = [];
  do {
    if (el.title) {
      return el.title;
    }
  } while ((el = el.parentElement) && el !== document.documentElement);
  return ''
}