mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-12-05 12:49:46 +08:00
feat(landing): 新增可嵌入的顶部横幅广告脚本
This commit is contained in:
1 parent
32251ce5be
commit
ffae671ca0
1 file changed
+215
@@ -0,0 +1,215 @@
|
||||
/*!
|
||||
* 百智办公 · 顶部横幅广告(单文件,无依赖)
|
||||
* 部署后以 <script src=".../ad-banner.js" async data-xxx="..."></script> 引用。
|
||||
* 默认配置已内置,只写 src 即可;需要改文案/落地页时用 data-* 覆盖。
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var el = document.currentScript || document.querySelector('script[src*="ad-banner"]');
|
||||
var d = (el && el.dataset) || {};
|
||||
|
||||
// ── 默认值 ──────────────────────────
|
||||
var DEF = {
|
||||
href: 'https://work.baizhi.cloud/',
|
||||
text: '百智办公 - 安全可控的企业 AI 办公工作台',
|
||||
highlight: '安全可控',
|
||||
highlightColor: '#ffe8a3',
|
||||
image: '',
|
||||
badge: '新品推荐',
|
||||
cta: '',
|
||||
height: 40,
|
||||
fontSize: 0,
|
||||
bg: '#bd3c10',
|
||||
color: '#ffffff',
|
||||
fixed: false,
|
||||
bottom: false,
|
||||
pushFixed: true,
|
||||
target: '_blank'
|
||||
};
|
||||
|
||||
function pick(name, fallback) {
|
||||
var v = d[name];
|
||||
return v === undefined || v === '' ? fallback : v;
|
||||
}
|
||||
function bool(name, fallback) {
|
||||
var v = d[name];
|
||||
if (v === undefined) return fallback;
|
||||
return v === '1' || v === 'true' || v === 'yes' || v === '';
|
||||
}
|
||||
|
||||
var C = {
|
||||
href: pick('href', DEF.href),
|
||||
text: pick('text', DEF.text),
|
||||
highlight: pick('highlight', DEF.highlight),
|
||||
highlightColor: pick('highlightColor', DEF.highlightColor),
|
||||
image: pick('image', DEF.image),
|
||||
badge: pick('badge', DEF.badge),
|
||||
cta: pick('cta', DEF.cta),
|
||||
height: Number(pick('height', DEF.height)),
|
||||
fontSize: Number(pick('fontSize', DEF.fontSize)),
|
||||
bg: pick('bg', DEF.bg),
|
||||
color: pick('color', DEF.color),
|
||||
fixed: bool('fixed', DEF.fixed),
|
||||
bottom: bool('bottom', DEF.bottom),
|
||||
pushFixed: bool('pushFixed', DEF.pushFixed),
|
||||
target: pick('target', DEF.target) === '_self' ? '_self' : '_blank'
|
||||
};
|
||||
|
||||
function safeUrl(v) {
|
||||
if (!v) return '';
|
||||
try {
|
||||
var u = new URL(v, location.href);
|
||||
return /^https?:$/.test(u.protocol) ? u.href : '';
|
||||
} catch (e) { return ''; }
|
||||
}
|
||||
function safeColor(v, fb) {
|
||||
return typeof v === 'string' && /^(#[0-9a-f]{3,8}|[a-z]{3,20})$/i.test(v.trim()) ? v.trim() : fb;
|
||||
}
|
||||
|
||||
var href = safeUrl(C.href);
|
||||
if (!href || document.getElementById('ad-banner-host')) return;
|
||||
|
||||
function mount() {
|
||||
if (!document.body || document.getElementById('ad-banner-host')) return;
|
||||
|
||||
var bs = getComputedStyle(document.body);
|
||||
var family = (bs.fontFamily || 'sans-serif').slice(0, 200).replace(/[;{}<>]/g, '');
|
||||
var weight = /^(normal|bold|bolder|lighter|[1-9]00)$/.test(bs.fontWeight) ? bs.fontWeight : '400';
|
||||
var bodyPx = parseFloat(bs.fontSize) || 16;
|
||||
var fontPx = C.fontSize > 0
|
||||
? Math.max(10, Math.min(24, C.fontSize))
|
||||
: Math.max(12, Math.min(15, bodyPx));
|
||||
var H = Math.max(24, Math.min(120, C.height || 40));
|
||||
|
||||
var host = document.createElement('div');
|
||||
host.id = 'ad-banner-host';
|
||||
host.style.cssText = 'all:initial;display:block;width:100%;flex:0 0 auto;box-sizing:border-box;' +
|
||||
'font-family:' + family + ';font-size:' + fontPx + 'px;font-weight:' + weight + ';' +
|
||||
(C.fixed
|
||||
? 'position:fixed;left:0;right:0;z-index:2147483000;' + (C.bottom ? 'bottom:0;' : 'top:0;')
|
||||
: 'position:relative;z-index:auto;');
|
||||
|
||||
var root = host.attachShadow ? host.attachShadow({ mode: 'open' }) : host;
|
||||
|
||||
var st = document.createElement('style');
|
||||
st.textContent =
|
||||
'.b{display:flex;align-items:center;justify-content:center;gap:8px;height:' + H + 'px;' +
|
||||
'padding:0 16px;font-family:inherit;font-size:inherit;font-weight:inherit;line-height:1.4;' +
|
||||
'text-decoration:none;cursor:pointer;transition:filter .15s}' +
|
||||
'.b:hover{filter:brightness(.9)}' +
|
||||
'.b:focus-visible{outline:3px solid #ffd400;outline-offset:-3px}' +
|
||||
'.badge{flex:none;border:1px solid currentColor;border-radius:3px;padding:1px 4px;' +
|
||||
'font-size:11px;line-height:1.2;opacity:.9}' +
|
||||
'.txt{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}' +
|
||||
'.cta{flex:none;font-weight:600;white-space:nowrap;text-decoration:underline;text-underline-offset:2px}' +
|
||||
'img{flex:none;height:' + Math.max(16, H - 12) + 'px;width:auto;max-width:30%;object-fit:contain}' +
|
||||
'@media(max-width:560px){.cta{display:none}.badge{font-size:10px}}';
|
||||
root.appendChild(st);
|
||||
|
||||
var a = document.createElement('a');
|
||||
a.className = 'b';
|
||||
a.href = href;
|
||||
a.target = C.target;
|
||||
a.rel = C.target === '_blank' ? 'noopener noreferrer sponsored' : 'sponsored';
|
||||
a.style.background = safeColor(C.bg, '#bd3c10');
|
||||
a.style.color = safeColor(C.color, '#ffffff');
|
||||
|
||||
if (C.badge) {
|
||||
var b = document.createElement('span');
|
||||
b.className = 'badge';
|
||||
b.textContent = C.badge;
|
||||
a.appendChild(b);
|
||||
}
|
||||
|
||||
var imgSrc = safeUrl(C.image);
|
||||
if (imgSrc) {
|
||||
var img = document.createElement('img');
|
||||
img.src = imgSrc;
|
||||
img.alt = '';
|
||||
img.onerror = function () { if (img.parentNode) img.parentNode.removeChild(img); sync(); };
|
||||
a.appendChild(img);
|
||||
}
|
||||
|
||||
var txt = document.createElement('span');
|
||||
txt.className = 'txt';
|
||||
// 用节点拼装,不拼 HTML,避免文案里的字符被解析成标签
|
||||
(function () {
|
||||
var full = C.text || '广告';
|
||||
var phrase = C.highlight || '';
|
||||
var idx = phrase ? full.indexOf(phrase) : -1;
|
||||
if (idx < 0) { txt.textContent = full; return; }
|
||||
if (idx > 0) txt.appendChild(document.createTextNode(full.slice(0, idx)));
|
||||
var hl = document.createElement('span');
|
||||
hl.style.color = safeColor(C.highlightColor, '#ffe8a3');
|
||||
hl.textContent = phrase;
|
||||
txt.appendChild(hl);
|
||||
var rest = full.slice(idx + phrase.length);
|
||||
if (rest) txt.appendChild(document.createTextNode(rest));
|
||||
})();
|
||||
a.appendChild(txt);
|
||||
|
||||
if (C.cta) {
|
||||
var c = document.createElement('span');
|
||||
c.className = 'cta';
|
||||
c.textContent = C.cta;
|
||||
a.appendChild(c);
|
||||
}
|
||||
root.appendChild(a);
|
||||
|
||||
var spacer = null;
|
||||
var ro = null;
|
||||
var shifted = [];
|
||||
|
||||
function sync() {
|
||||
if (spacer) spacer.style.height = host.offsetHeight + 'px';
|
||||
}
|
||||
|
||||
function pushTopAnchored(delta) {
|
||||
if (!C.fixed || !C.pushFixed) return;
|
||||
var vw = window.innerWidth;
|
||||
var nodes = document.body.getElementsByTagName('*');
|
||||
for (var i = 0; i < nodes.length && shifted.length < 40; i++) {
|
||||
var node = nodes[i];
|
||||
if (node.closest && node.closest('#ad-banner-host')) continue;
|
||||
var cs = getComputedStyle(node);
|
||||
if (cs.position !== 'fixed' && cs.position !== 'sticky') continue;
|
||||
if (cs.top !== '0px') continue;
|
||||
var r = node.getBoundingClientRect();
|
||||
if (r.width < vw * 0.3 || r.height > 200) continue;
|
||||
shifted.push([node, node.style.top]);
|
||||
node.style.top = delta + 'px';
|
||||
}
|
||||
}
|
||||
|
||||
if (C.fixed) {
|
||||
spacer = document.createElement('div');
|
||||
spacer.setAttribute('data-ad-banner-spacer', '');
|
||||
spacer.style.cssText = 'display:block;width:100%;height:0;margin:0;padding:0;border:0;';
|
||||
if (C.bottom) document.body.appendChild(spacer);
|
||||
else document.body.insertBefore(spacer, document.body.firstChild);
|
||||
document.body.appendChild(host);
|
||||
sync();
|
||||
window.addEventListener('resize', sync);
|
||||
if (window.ResizeObserver) { ro = new ResizeObserver(sync); ro.observe(a); }
|
||||
pushTopAnchored(H);
|
||||
} else {
|
||||
// 文档流:横幅占真实布局空间,随页面滚走,不遮挡站点 header
|
||||
document.body.insertBefore(host, document.body.firstChild);
|
||||
}
|
||||
|
||||
window.__adBanner = {
|
||||
destroy: function () {
|
||||
if (ro) ro.disconnect();
|
||||
window.removeEventListener('resize', sync);
|
||||
if (spacer && spacer.parentNode) spacer.parentNode.removeChild(spacer);
|
||||
if (host.parentNode) host.parentNode.removeChild(host);
|
||||
shifted.forEach(function (p) { p[0].style.top = p[1]; });
|
||||
window.__adBanner = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (document.body) mount();
|
||||
else document.addEventListener('DOMContentLoaded', mount, { once: true });
|
||||
})();
|
||||
Reference in new issue
Block a user