You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何拦截并重写JavaScript中的isPermitted原型方法?

解决建议

核心问题

你之前的intercept函数仅针对对象的直接属性生效,但isPermitted是定义在构造函数原型(n.prototype)上的方法,属于原型链属性,所以原有拦截逻辑无法触达。

可行实现方案

在拦截到Subscriptions构造函数后,直接修改其原型上的isPermitted方法即可,示例代码如下:

function intercept(obj, prop, callback) {
  var oldVal = null;
  Object.defineProperty(obj, prop, {
    get: function() { return oldVal; },
    set: function(newVal) {
      callback.call(this, newVal);
      oldVal = newVal;
    },
    enumerable: true,
    configurable: true
  });
};

intercept(Opta, 'Subscriptions', function(Subscriptions) {
  // 改写静态方法isPermittedDomain
  Subscriptions.isPermittedDomain = function() { return true; };

  // 改写原型方法isPermitted
  const originalIsPermitted = Subscriptions.prototype.isPermitted;
  Subscriptions.prototype.isPermitted = function(t) {
    // 自定义逻辑:示例直接返回true跳过权限检查
    return true;
    
    // 若需保留原逻辑并修改结果,可调用原方法:
    // const originalResult = originalIsPermitted.call(this, t);
    // return originalResult; // 或修改后返回
  };

  // 原有的feeds拦截逻辑
  intercept(Subscriptions, 'feeds', function(feeds) {
    const t = Opta.Trans.loadTerms({term_set_id: Opta.Trans.mapSportIdToTerms(1)});
    Opta.when(t).done(startReact);
  });
});

关键说明

  1. 时机确保:必须保证你的代码在v3.opta-widgets.js加载完成、Subscriptions.prototype.isPermitted已定义后执行,否则会覆盖为undefined。若原脚本异步加载,可将你的代码放在window.onload或动态监听脚本加载完成的回调中。
  2. 构造函数确认:如果Subscriptions不是目标构造函数n,可通过实例.constructor.prototype定位(比如拿到Subscriptions的实例后,用instance.constructor.prototype.isPermitted修改)。

内容的提问来源于stack exchange,提问作者MisterButter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 00:56:19