/** * Open Graph:复用 / meta[name=description],补齐绝对 og:url / og:image。 * 爬虫可读 head 内静态 meta;本脚本在浏览器中按当前域名校正。 */ (function () { var PROD_ORIGIN = 'https://ideship.com'; var OG_IMAGE_PATH = '/site/assets/img/og-cover.png'; var OG_IMAGE_REL = 'assets/img/og-cover.png'; function truncate(text, max) { var value = String(text || '') .replace(/\s+/g, ' ') .trim(); if (value.length <= max) return value; return value.slice(0, max); } function ensureMeta(property, content) { if (!content) return; var node = document.querySelector('meta[property="' + property + '"]'); if (!node) { node = document.createElement('meta'); node.setAttribute('property', property); document.head.appendChild(node); } node.setAttribute('content', content); } function resolveOrigin() { if (location.protocol === 'http:' || location.protocol === 'https:') { return location.origin; } return PROD_ORIGIN; } function resolvePageUrl(origin) { if (location.protocol === 'file:') { return /9610\.html$/i.test(location.pathname || '') ? PROD_ORIGIN + '/9610.html' : PROD_ORIGIN + '/'; } var path = location.pathname || '/'; if (/\/index\.html$/i.test(path)) { path = path.replace(/\/index\.html$/i, '/') || '/'; } if (path === '/site' || path === '/site/' || path === '/site/index.html') { path = '/'; } return origin + path + (location.search || ''); } function resolveImageUrl(origin) { if (origin === PROD_ORIGIN || /(^|\.)ideship\.com$/i.test(location.hostname || '')) { return origin + OG_IMAGE_PATH; } try { return new URL(OG_IMAGE_REL, location.href).href; } catch (e) { return origin + '/assets/img/og-cover.png'; } } function applyOpenGraph() { var title = truncate(document.title, 30); var descNode = document.querySelector('meta[name="description"]'); var description = truncate(descNode && descNode.getAttribute('content'), 60); var origin = resolveOrigin(); ensureMeta('og:title', title); if (description) ensureMeta('og:description', description); ensureMeta('og:image', resolveImageUrl(origin)); ensureMeta('og:url', resolvePageUrl(origin)); ensureMeta('og:type', 'website'); } applyOpenGraph(); window.IDEOpenGraph = { refresh: applyOpenGraph }; })();