0

After reading this..

https://stackoverflow.com/a/54096072/13099685

Could anyone recommend me a way to use it in greasmonkey or as a Firefox extension, for rewriting the header data parsed to webpages (UA switching).

What i have so far is..

manifest.json

{

  "description": "",
  "manifest_version": 2,
  "name": "user-agentr",
  "version": "1.0",
  "icons": {
    "48": "icons/person-48.png"
  },

  "permissions": [
    "webRequest", "webRequestBlocking", "https://*" 
  ],

  "background": {
    "scripts": ["background.js"]
  },

  "browser_action": {
    "default_icon": "icons/person-32.png",
    "default_title": "Choose a user agent",
    "default_popup": "popup/choose_ua.html"
  }

}

background.js

function setUserAgent(userAgent) {
    Object.defineProperty(navigator, "userAgent", { 
        get: function () { 
            return userAgent; // customized user agent
        },
        configurable: true
    });
}

// Now in your setup phase:
// Keep the initial value
var initialUserAgent = navigator.userAgent;
setUserAgent('foo');

// In your tearDown:
// Restore the initial value
// setUserAgent(initialUserAgent);

--

and is this all I need to successfully change a user agent?

Thanks.

ps. I'm comparing the values using

<!DOCTYPE html>
<html>
<body>

<div id="demo"></div>

<script>
var txt = "";
txt += "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt += "<p>Browser Name: " + navigator.appName + "</p>";
txt += "<p>Browser Version: " + navigator.appVersion + "</p>";
txt += "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt += "<p>Browser Language: " + navigator.language + "</p>";
txt += "<p>Browser Online: " + navigator.onLine + "</p>";
txt += "<p>Platform: " + navigator.platform + "</p>";
txt += "<p>User-agent header: " + navigator.userAgent + "</p>";

document.getElementById("demo").innerHTML = txt;
</script>

</body>
</html>

and

<html>
<head>
</head>

<body>


<?php

    $headers = apache_request_headers();

    foreach ($headers as $header => $value) {
        echo "$header: $value <br />\n";
    }

echo "<hr>";
echo $_SERVER['HTTP_USER_AGENT']
?>

</body>

</html>
Zoroz
  • 1
  • 1
  • Remove background.js and use a `content script` that runs at `document_start` and redefines wrappedJSObject.navigator (the definition should use `cloneInto`, see [MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts) for details). – wOxxOm Mar 21 '20 at 15:49
  • https://pastebin.com/QiN96hyu & https://pastebin.com/kVwcm6eQ ~ keeps giving me errors "Protocol error (unknownError): JSON.parse: expected ',' or '}' after property value in object at line 22 column 3 of the JSON data" – Zoroz Mar 21 '20 at 16:36
  • 1) There should be a target in cloneInto, IIRC it's `document.defaultView` 2) `"*://*"` lacks a path so I don't think it'll match anything, should be `"*://*/*"` 3) I have no idea who gives you those errors but your code assigns a sub-property `userAgent` to the entire `navigator`, which is definitely wrong. – wOxxOm Mar 21 '20 at 16:47
  • Note, if you want to change UA in outgoing requests, this won't help. You need to do it via browser.webRequest API. – wOxxOm Mar 21 '20 at 16:53

0 Answers0