Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有iFrame的自定义用户代理_Javascript_Html_Iframe_Google Chrome Extension - Fatal编程技术网

Javascript 带有iFrame的自定义用户代理

Javascript 带有iFrame的自定义用户代理,javascript,html,iframe,google-chrome-extension,Javascript,Html,Iframe,Google Chrome Extension,是的,我读过,但我不明白如何正确实施/它不起作用 我正在尝试在chrome扩展中创建一个带有自定义用户代理的Iframe,这是我当前的代码(来自其他教程/答案) 函数setUserAgent(窗口,userAgent){ if(window.navigator.userAgent!=userAgent){ var userAgentProp={get:function(){return userAgent;}}; 试一试{ 定义属性(window.navigator、'userAgent',u

是的,我读过,但我不明白如何正确实施/它不起作用

我正在尝试在chrome扩展中创建一个带有自定义用户代理的Iframe,这是我当前的代码(来自其他教程/答案)

函数setUserAgent(窗口,userAgent){
if(window.navigator.userAgent!=userAgent){
var userAgentProp={get:function(){return userAgent;}};
试一试{
定义属性(window.navigator、'userAgent',userAgentProp);
}捕获(e){
window.navigator=Object.create(navigator{
userAgent:userAgentProp
});
}
}
}
addEventListener(“加载”,函数(){
document.querySelector('iframe').contentWindow,“测试用户代理”;
});
标题
您需要

1a。插入内容脚本

它可以通过编程方式从扩展页注入iframe

manifest.json

"permissions": ["webNavigation"]
index.js:

chrome.tabs.getCurrent(tab=>{
chrome.webNavigation.onCommitted.addListener(函数onCommitted(info)){
if(info.tabId==tab.id){
chrome.webNavigation.onCommitted.removeListener(onCommitted);
chrome.tabs.executeScript({
frameId:info.frameId,
文件:“content.js”,
runAt:“文档开始”,
});
}
}, {
url:[{urlEquals:document.querySelector('iframe').src}],
});
});
1b。声明内容脚本

请注意,前面的方法理论上可能允许首先运行iframe的
中的一些内联脚本。在这种情况下,您必须向iframe
src
(例如
https://example.org/page/page?foobar
)并在manifest.json中使用:

"content_scripts": [{
  "matches": ["*://example.org/*?foobar*"],
  "all_frames": true,
  "run_at": "document_start",
  "js": ["content.js"]
}],
2。添加页面脚本

要立即运行我们的代码,让我们通过textContent运行它。我们没有为页面代码使用单独的文件,因为这样会将其放入队列,在队列中它可以在当前挂起的页面脚本之后运行

content.js:

var script=document.createElement('script');
script.textContent='('+函数(){
Object.defineProperty(导航器,'用户代理'{
得到(){
返回“测试用户代理”;
},
});
} + ')();';
(document.head | | document.documentElement).appendChild(脚本);
script.remove();

我应该如何使其匹配?因为我只想更改某些窗口的用户代理,而不是所有选项卡(甚至我的所有扩展窗口),但匹配需要URL