如何为Fluid Player注入默认skipoffset实现广告跳过按钮强制展示
可行的注入环节和实现方案
方案1:全局拦截XMLHttpRequest的responseXML读取逻辑
你之前使用的播放器提供的onBeforeXMLHttpRequestOpen和onBeforeXMLHttpRequest钩子时机晚于播放器内部onreadystatechange的解析逻辑,因此可以从底层XHR层面提前拦截响应内容:
- 检测到请求为VAST广告请求时,在响应返回后、播放器读取
responseXML之前,向XML的<Linear>节点注入skipoffset属性 - 实现代码示例:
const originalXhrOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function(method, url) { // 可根据实际业务的VAST请求特征调整匹配规则 const isVastAdRequest = /vast|advertisement|ad/i.test(url); if (isVastAdRequest) { this.addEventListener('load', () => { if (!this.responseXML) return; const linearAdNodes = this.responseXML.querySelectorAll('Linear'); linearAdNodes.forEach(node => { // 仅当原生没有配置skipoffset时注入默认5秒的跳过间隔 if (!node.hasAttribute('skipoffset')) { node.setAttribute('skipoffset', '00:00:05'); } }); // 覆写responseXML,让播放器拿到修改后的XML内容 Object.defineProperty(this, 'responseXML', { value: this.responseXML, configurable: true }); }, { once: true }); } return originalXhrOpen.apply(this, arguments); };
该方案时机早于播放器内部的属性解析逻辑,播放器会直接使用你注入的skipoffset值渲染跳过按钮。
方案2:劫持播放器VAST解析方法
如果你可以拿到fluid player的实例,可以直接重写其VAST模块的线性广告解析逻辑,在解析阶段补全默认值:
// 替换为你实际的播放器实例 const playerInstance = fluidPlayer('your-video-element-id'); const originalParseLinear = playerInstance.vast.parseLinearAd; playerInstance.vast.parseLinearAd = function(adNode) { if (!adNode.hasAttribute('skipoffset')) { adNode.setAttribute('skipoffset', '00:00:05'); } return originalParseLinear.apply(this, arguments); };
内容的提问来源于stack exchange,提问作者Jon Nir
相关产品推荐
相关产品推荐

