图像预览修正 - telegra.ph

通过更改引用来源规则来让 telegra.ph 的图片能正常加载。

// ==UserScript==
// @name        图像预览修正 - telegra.ph
// @namespace   uk.jixun
// @match       https://telegra.ph/*
// @grant       none
// @version     1.0
// @author      Jixun
// @run-at      document-start
// @description 通过更改引用来源规则来让 telegra.ph 的图片能正常加载。
// @license     MIT
// ==/UserScript==

const BURST_TIMER_THRESHOLD = 1000 * 60 * 60 * 30; // keep the same burst timer for 30 days
const CLEAR_REFERRER_ORIGINS = new Set([
  'https://mmbiz.qpic.cn'
]);

function main() {
  const now = Date.now();
  let cacheBurstTimer = localStorage.cacheBurstTimer || (localStorage.cacheBurstTimer = now);
  if (now - parseInt(cacheBurstTimer, 10) > BURST_TIMER_THRESHOLD) {
    localStorage.cacheBurstTimer = cacheBurstTimer = now;
  }

  // Cache burst without referrer
  for(const img of document.querySelectorAll('img')) {
    img.referrerPolicy = 'no-referrer';
    img.loading = 'lazy';
    const url = new URL(img.src);
    const shouldBurstReferrer = CLEAR_REFERRER_ORIGINS.has(url.origin);
    if (shouldBurstReferrer) {
      url.search += `${url.search ? `&` : '?'}_=${cacheBurstTimer}`;
      img.src = String(url);
    }
  }
}

addEventListener('DOMContentLoaded', main);
window?.body && main();