我有这段代码来记录对一堆 window.navigator 属性的调用:
var navigatorProperties = [ "appCodeName", "appMinorVersion", "appName", "appVersion", "cookieEnabled", "cpuClass", "onLine", "opsProfile", "platform", "product", "systemLanguage", "userAgent", "userLanguage", "userProfile" ];
var contentStorage = {};
navigatorProperties.forEach(
function(property) {
contentStorage["window.navigator." + property] = window.navigator[property];
Object.defineProperty(window.navigator, property, {
configurable: true,
get: function() { logValue("window.navigator." + property, contentStorage["window.navigator." + property], "get"); return contentStorage["window.navigator." + property] },
set: function(value) { logValue("window.navigator." + property, contentStorage["window.navigator." + property], "set"); contentStorage["window.navigator." + property] = value; }
});
});
当此代码位于常规网页中时,对已定义和监视属性的任何调用,例如 console.log('local page: ' + window.navigator.product); 工作正常,也就是说,我得到 logValue 执行输出。但是,我希望此代码存在于插件 SDK 构建的扩展内容注入脚本中,该脚本通过页面 mod API 注入。当我这样做并转到修改或访问我观察的属性的页面时,永远不会触发 logValue。我试过用 unsafeWindow 替换 window 但也没有运气。任何人都可以告诉我为什么通过插件注入此代码时该代码不起作用?