英文:
How can I spoof the search in new tab as mobile (Edge Extension)? Javascript
问题
I am not so good at JavaScript, but I wanted to try making the extension for Microsoft Edge where it'll randomly search in bing. The desktop search is working for now, but Mobile search isn't able to spoof the search as a mobile device. How can I solve it? I have manifest version 3.
This is my function which searches in a new tab:
function executeSearchM(searches){
/*
Spoof to mobile, not working :(
*/
for(var i=0; i<searches.length; i++){
var win = window.open('https://www.bing.com/search?q='+searches[i],'_blank');
win.focus();
}
}
Search is working but it is still in Desktop mode. What should I do to spoof it as a mobile device search.
This is the spoof function:
function spoof(){
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'User-Agent') {
details.requestHeaders.splice(i, 1);
break;
}
}
details.requestHeaders.push({name: 'User-Agent', value: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Mobile Safari/537.36'});
return {requestHeaders: details.requestHeaders};
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]
);
}
I tried lots of things to spoof as a device but it is not working, I am not good at JavaScript that's why I am not able to do it properly.
英文:
I am not so good at JavaScript, but I wanted to try making the extension for Microsoft Edge where it'll randomly search in bing. The desktop search is working for now, but Mobile search isn't able to spoof the search as mobile device. How can I solve it? I have manifest version 3.
This is my function which search in new tab:
function executeSearchM(searches){
/*
Spoof to mobile, not working :(
*/
for(var i=0; i<searches.length; i++){
var win = window.open('https://www.bing.com/search?q='+searches[i],'_blank');
win.focus();
}
}
Search is working but it is still in Desktop mode. What should I do to spoof it as mobile device search.
This is spoof function:
function spoof(){
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'User-Agent') {
details.requestHeaders.splice(i, 1);
break;
}
}
details.requestHeaders.push({name: 'User-Agent', value: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Mobile Safari/537.36'});
return {requestHeaders: details.requestHeaders};
},
{urls: ["<all_urls>"]},
["blocking", "requestHeaders"]
);
}
I tried lots of things to spoof as a device but it is not working, I am not good at JavaScript that's why I am not able to do it properly.
答案1
得分: 1
你正在使用 webRequestBlocking
来编辑 User-Agent,但 webRequestBlocking
需要 Manifest 版本为 2 或更低。在 Manifest V3 中,您需要使用 declarativeNetRequest 来编辑 User-Agent。
您可以参考以下步骤根据您的情况修改代码。
首先,您需要在 manifest.json 中添加权限:
"permissions": [
"webRequest",
"declarativeNetRequest",
"tabs"
],
"host_permissions": [
"<all_urls>"
],
然后,在 popup.js 中编辑 executeSearchD
和 executeSearchM
函数以发送 editUserAgent
消息和 resetUserAgent
消息:
function executeSearchD(searches){
const response = chrome.runtime.sendMessage("resetUserAgent"); //添加此行
for(var i=0; i<searches.length; i++){
var win = window.open('https://www.bing.com/search?q='+searches[i]);
win.focus();
}
}
function executeSearchM(searches){
const response = chrome.runtime.sendMessage("editUserAgent"); //添加此行
for(var i=0; i<searches.length; i++){
var win = window.open('https://www.bing.com/search?q='+searches[i],'_blank');
win.focus();
}
}
最后,在 background.js 中设置 User-Agent 编辑规则并添加事件监听器来更改 User-Agent:
const RULE = {
"id": 1,
"priority": 1,
"action": {
"type": "modifyHeaders",
"requestHeaders": [
{ "header": "User-Agent", "operation": "set", "value": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36 Edg/112.0.1722.48" }
]
},
"condition": { "urlFilter": "www.bing.com", "resourceTypes": ["main_frame"] }
};
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message == 'editUserAgent') {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [RULE.id],
addRules: [RULE]
});
chrome.tabs.reload();
} else if (message === 'resetUserAgent') {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [RULE.id]
});
chrome.tabs.reload();
} else {
sendResponse(message);
}
sendResponse({response: "来自后台脚本的响应"});
});
结果如下图所示:
英文:
You're using webRequestBlocking
to edit User-Agent, but webRequestBlocking
requires Manifest version of 2 or lower. In Manifest V3, you need to use declarativeNetRequest to edit User-Agent.
You can refer to below steps and modify codes according to your situation.
First, you need to add permissions in manifest.json:
"permissions": [
"webRequest",
"declarativeNetRequest",
"tabs"
],
"host_permissions": [
"<all_urls>"
],
Then edit the executeSearchD
and executeSearchM
functions in popup.js to send the editUserAgent message and resetUserAgent message:
function executeSearchD(searches){
const response = chrome.runtime.sendMessage("resetUserAgent"); //add this line
for(var i=0; i<searches.length; i++){
var win = window.open('https://www.bing.com/search?q='+searches[i]);
win.focus();
}
}
function executeSearchM(searches){
const response = chrome.runtime.sendMessage("editUserAgent"); //add this line
for(var i=0; i<searches.length; i++){
var win = window.open('https://www.bing.com/search?q='+searches[i],'_blank');
win.focus();
}
}
Finally, we set a User-Agent edit rule and add an event listener in background.js to change the User-Agent:
const RULE = {
"id": 1,
"priority": 1,
"action": {
"type": "modifyHeaders",
"requestHeaders": [
{ "header": "User-Agent", "operation": "set", "value": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36 Edg/112.0.1722.48" }
]
},
"condition": { "urlFilter": "www.bing.com", "resourceTypes": ["main_frame"] }
};
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message == 'editUserAgent') {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [RULE.id],
addRules: [RULE]
});
chrome.tabs.reload();
} else if (message === 'resetUserAgent') {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [RULE.id]
});
chrome.tabs.reload();
} else {
sendResponse(message);
}
sendResponse({response: "response from background script"});
});
The result is like below:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论