我试图使用简单的chrome扩展名重载navigator.userAgent.由于内容脚本在隔离的env中运行,我尝试创建一个脚本元素并将逻辑写入此脚本.这发生在扩展的后台页面
chrome.tabs.query({
active:!0
}, function(tabs) {
var x = "window.navigator.__defineGetter__('userAgent', function() {" +
"return 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D)" +
" AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile " +
"Safari/535.19'; });console.log(navigator.userAgent);";
for (var i = 0;i < tabs.length;i++) {
var code = 'var s = document.createElement("script"); s.text = "' + x +
'"; document.head.insertBefore(s, document.head.firstChild);' +
'navigator.userAgent ="s"; console.log(navigator.userAgent);';
// Inject into the tabs of choice - currently everything.
chrome.tabs.executeScript(tabs[i].id, {
code: code
});
}
});
Run Code Online (Sandbox Code Playgroud)
脚本附加了head元素,我可以看到UA字符串是通过在chrome的控制台中尝试navigator.userAgent而被欺骗的字符串,因此我相信导航器对象已经过载. …