如何在新标签页中模拟移动设备搜索(Edge扩展)? JavaScript

huangapple go评论53阅读模式
英文:

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.

Github Project

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.

Github Prokect

This is my function which search in new tab:

function executeSearchM(searches){
    /*
    Spoof to mobile, not working :(
    */
    for(var i=0; i&lt;searches.length; i++){

        var win = window.open(&#39;https://www.bing.com/search?q=&#39;+searches[i],&#39;_blank&#39;);
        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 &lt; details.requestHeaders.length; ++i) {
                if (details.requestHeaders[i].name === &#39;User-Agent&#39;) {
                    details.requestHeaders.splice(i, 1);
                    break;
                }
            }
            details.requestHeaders.push({name: &#39;User-Agent&#39;, value: &#39;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&#39;});
            return {requestHeaders: details.requestHeaders};
        },
        {urls: [&quot;&lt;all_urls&gt;&quot;]},
        [&quot;blocking&quot;, &quot;requestHeaders&quot;]
    );
}

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 中编辑 executeSearchDexecuteSearchM 函数以发送 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: "来自后台脚本的响应"});
});

结果如下图所示:

如何在新标签页中模拟移动设备搜索(Edge扩展)? JavaScript

英文:

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:

&quot;permissions&quot;: [
    &quot;webRequest&quot;,
    &quot;declarativeNetRequest&quot;,
    &quot;tabs&quot;
  ],
  &quot;host_permissions&quot;: [
    &quot;&lt;all_urls&gt;&quot;
  ],

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(&quot;resetUserAgent&quot;); //add this line
    for(var i=0; i&lt;searches.length; i++){
        var win = window.open(&#39;https://www.bing.com/search?q=&#39;+searches[i]);
        win.focus();
    }
}

function executeSearchM(searches){
    const response = chrome.runtime.sendMessage(&quot;editUserAgent&quot;); //add this line 
    for(var i=0; i&lt;searches.length; i++){ 
        var win = window.open(&#39;https://www.bing.com/search?q=&#39;+searches[i],&#39;_blank&#39;);
        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 = {
    &quot;id&quot;: 1,
    &quot;priority&quot;: 1,
    &quot;action&quot;: {
      &quot;type&quot;: &quot;modifyHeaders&quot;,
      &quot;requestHeaders&quot;: [
        { &quot;header&quot;: &quot;User-Agent&quot;, &quot;operation&quot;: &quot;set&quot;, &quot;value&quot;: &quot;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&quot; }
      ]
    },
    &quot;condition&quot;: { &quot;urlFilter&quot;: &quot;www.bing.com&quot;, &quot;resourceTypes&quot;: [&quot;main_frame&quot;] }
};

chrome.runtime.onMessage.addListener((message, sender, sendResponse) =&gt; {
    if (message == &#39;editUserAgent&#39;) {
        chrome.declarativeNetRequest.updateDynamicRules({
            removeRuleIds: [RULE.id],
            addRules: [RULE]
          });
          chrome.tabs.reload();
    } else if (message === &#39;resetUserAgent&#39;) {
        chrome.declarativeNetRequest.updateDynamicRules({
            removeRuleIds: [RULE.id]
          });
        chrome.tabs.reload();
      } else {
        sendResponse(message);
      }
      sendResponse({response: &quot;response from background script&quot;});
});

The result is like below:

如何在新标签页中模拟移动设备搜索(Edge扩展)? JavaScript

huangapple
  • 本文由 发表于 2023年4月17日 16:27:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定