0

I know there are one or two posts out there concerning a similar question but the answers never helped me or it just didn't work. I wanted to create a chrome extension that has changing a user agent being a part of it (I know there are user-agent changer extensions out there but that's not what I'm trying to make). Thanks

4
  • Have you even read that short paragraph I wrote...? I literally put on the first sentence that I knew there were already one or two posts on this but they either did not work or did not help me. I also said that I want to make a chrome extension but one part of it needs to change the user agent. So thanks for wasting my time and yours... Commented Mar 24, 2016 at 2:40
  • Banks, at Stackoverflow we expect either a minimal complete verifiable example and/or at least a clear problem description including a "tried step 1, 2, 3, expected this, got that instead". Note that often a wrong expectation leads to a "does not work" while the observed behaviour is actually correct. Only an example, as short as possible or clear descriptions help to solve this. If you already tried answers or resources you're welcome to add them to your post and explain the problems with them. Commented Mar 24, 2016 at 8:42
  • Finally your question and its answers should also be useful for others. If there are non working or incomplete resources it's also up to you creating a better resource by making a complete and clear question and accepting the answer that helped you. You may also wait serveral days before accepting to watch the votes to the answers (but accepting answers is a vital part, you should not forget it). Commented Mar 24, 2016 at 8:50
  • Ok thanks, I get it :) Commented Mar 24, 2016 at 21:00

1 Answer 1

4

You will need the webRequest API.

User agent can be changed in the onBeforeSendHeaders event. In fact, the documentation has a very pertinent example in it.

  chrome.webRequest.onBeforeSendHeaders.addListener(
    function(details) {
      for (var i = 0; i < details.requestHeaders.length; ++i) {
        if (details.requestHeaders[i].name === 'User-Agent') {
          /* change details.requestHeaders[i].value here */
          break;
        }
      }
      return {requestHeaders: details.requestHeaders};
    },
    {urls: ["<all_urls>"]},
    ["blocking", "requestHeaders"]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but this didn't help me that much considering the fact that webRequest API don't work with content scripts.
That's a problem of not understanding how extensions work in general. Which is not something one can easily fix in a few sentences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.