简体   繁体   English

更改Chrome扩展程序的URL用户代理

[英]Change Chrome Extension User Agent for URL

I'm currently building a Chrome extension and I would like to change the user agent of the Chrome extension when requesting a file from the server but it keeps requesting it with the default Chrome user agent. 我目前正在构建一个Chrome扩展程序,并且我想在从服务器请求文件时更改Chrome扩展程序的用户代理,但它始终使用默认的Chrome用户代理来请求文件。 I've used Chrome extension plugins source code to change on request and it still will not happen. 我已使用Chrome扩展插件的源代码按要求进行更改,但仍然不会发生。 I've inserted a .htaccess only allowing one specific user agent and I would like to see if I can even do it with Chrome extensions. 我插入的.htaccess仅允许一个特定的用户代理,我想看看我是否可以使用Chrome扩展程序进行操作。

This is the JS code I'm using to get the file from the server. 这是我用来从服务器获取文件的JS代码。

var imported = document.createElement('script');
imported.src = 'FILE BEING IMPORTED/REQUESTED';
document.head.appendChild(imported);

I've also tried this and set the permissions correctly in manifest.json 我也尝试过此操作,并在manifest.json正确设置权限

chrome.webRequest.onBeforeSendHeaders.addListener(function(details){
    for(var i=0; i < details.requestHeaders.length; ++i){
        if(details.requestHeaders[i].name === "User-Agent"){
            details.requestHeaders[i].value = "Desired User Agent Here";

            break;
        }
    }
    return {requestHeaders: details.requestHeaders};
}, {urls: ["<all_urls>"]}, ["blocking", "requestHeaders"]);

I tested your code and it works. 我测试了您的代码,它可以正常工作。 Are you sure you have both the "webRequest" and "webRequestBlocking" permissions plus the correct host permissions in your manifest file? 您确定清单文件中同时具有“ webRequest”和“ webRequestBlocking”权限以及正确的主机权限吗? It should not make a difference but are you including the imported script in an extension page? 不会有什么不同,但是您是否在扩展页面中包括了导入的脚本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM