Skip to content

Instantly share code, notes, and snippets.

@noromanba
Last active February 22, 2017 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noromanba/f71a9da49f2e44b5c9fc0645f15c05f3 to your computer and use it in GitHub Desktop.
Save noromanba/f71a9da49f2e44b5c9fc0645f15c05f3 to your computer and use it in GitHub Desktop.
shrink too long title attribute for UserScript
// ==UserScript==
// @name title attr shrinker
// @namespace http://noromanba.flavors.me
// @description shrink too long title attribute for UserScript
// @include http://example.com/DIY
// @grant none
// @noframes
// @run-at document-end
// @version 2017.2.22.1
// @homepage https://gist.github.com/noromanba/f71a9da49f2e44b5c9fc0645f15c05f3
// @downloadURL https://gist.github.com/noromanba/f71a9da49f2e44b5c9fc0645f15c05f3/raw/title-attr-shrinker.user.js
// @license MIT License https://nrm.mit-license.org/2017
// @author noromanba http://noromanba.flavors.me
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/3/39/Internet-group-chat.svg/128px-Internet-group-chat.svg.png
// ==/UserScript==
// Icon (PD by By Jakub Steiner/Tango! project)
// https://commons.wikimedia.org/wiki/File%3AInternet-group-chat.svg
// Devel
// https://gist.github.com/noromanba/f71a9da49f2e44b5c9fc0645f15c05f3
// Bookmarklet
// http://let.hatelabo.jp/noromanba/let/hJmeyeG6sc9H
// via
// http://let.hatelabo.jp/a-kuma3/let/hJmeyNv-p802
// http://q.hatena.ne.jp/1487159409
// http://q.hatena.ne.jp/images/question/1487159/1487159409.jpg
// hatenanews on http://www.hatena.ne.jp/ has been fixed
(() => {
'use strict';
Array.from(document.body.querySelectorAll([
// write <a> selector(s) as you like
]), (link, idx) => {
if (!link.href) return;
const shrink = () => {
const xhr = new XMLHttpRequest();
xhr.open('GET', link.href);
xhr.timeout = 3000;
xhr.responseType = 'document';
xhr.addEventListener('load', () => {
if (xhr.readyState !== xhr.DONE || xhr.status !== 200) return;
const ctx = xhr.response.document;
const title = (ctx.querySelector([
'head title'
]) || {}).textContent || '';
const descr = (ctx.querySelector([
'head meta[property="og:description"][content]',
'head meta[name="twitter:description"][content]',
]) || {}).content || '';
const shrinked = title + '\n' + descr;
link.title = shrinked;
});
xhr.send();
};
const INTERVAL = 500;
setTimeout(shrink, INTERVAL * idx);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment