1

I'm trying to have my extension only work while in incognito mode. I can't seem to get it to work by adding "incognito": "split" to my manifest.json, and adding an if statement in the background.js looking for chrome.extension.inIncognitoContext.

Edit:

Okay, so what I'm trying to do is change my user-agent headers while only in incognito. I'm doing this based off this: http://elaineou.com/2016/02/19/how-to-use-chrome-extensions-to-bypass-paywalls/

It works for the said link above, but I can't seem to get it to work for all website while in incognito mode. This also won't be released, and I do have the allowed in incognito button checked.

manifest.json

{
  "name": "Incognito Chrome Extension",
  "version": "0.1",
  "description": "This is an incognito chrome extension.",
  "incognito": "split",
  "permissions": ["webRequest", "webRequestBlocking",
                   "http://localhost:3000/",
                   "http://*/*",
                   "https://*/*"
                  ],
  "background": {
    "scripts": ["background.js"]
  },
  "manifest_version": 2
}

Background.js

var ALLOW_COOKIES = [""];
if(chrome.extension.inIncognitoContext){
    function changeRefer(details) {
    foundReferer = false;
    foundUA = false;

    var reqHeaders = details.requestHeaders.filter(function(header) {
      // block cookies by default
      if (header.name !== "Cookie") {
        return header;
      } 

      allowHeader = ALLOW_COOKIES.map(function(url) {
        if (details.url.includes(url)) {
          return true;
        }
        return false;
      });
      if (allowHeader.reduce(function(a, b) { return a || b}, false)) { return header; }

    }).map(function(header) {

      if (header.name === "Referer") {
        header.value = "https://www.google.com/";
        foundReferer = true;
      }
      if (header.name === "User-Agent") {
        header.value = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";
        foundUA = true;
      }
      return header;
    });

    // append referer
    if (!foundReferer) {
      reqHeaders.push({
        "name": "Referer",
        "value": "https://www.google.com/"
      });
    }
    if (!foundUA) {
      reqHeaders.push({
        "name": "User-Agent",
        "value": "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"
      });
    }
    console.log(reqHeaders);
    return {requestHeaders: reqHeaders};
  }

  function blockCookies(details) {
    for (var i = 0; i < details.responseHeaders.length; ++i) {
      if (details.responseHeaders[i].name === "Set-Cookie") {
        details.responseHeaders.splice(i, 1);
      }
    }
    return {responseHeaders: details.responseHeaders};
  }

  chrome.webRequest.onBeforeSendHeaders.addListener(changeRefer, {
    urls: ["<all_urls>"],
    types: ["main_frame"],
  }, ["requestHeaders", "blocking"]);

  chrome.webRequest.onHeadersReceived.addListener(blockCookies, {
    urls: ["<all_urls>"],
    types: ["main_frame"],
  }, ["responseHeaders", "blocking"]);
}
Jack Rothrock
  • 407
  • 1
  • 8
  • 21

1 Answers1

1

I guess that your concern is the extra redundant background page process?

Add "incognito": "split" in the manifest file and close the extension's background page in non-incognito mode:

// background page or event page:
if (!chrome.extension.inIncognitoContext) {
    window.close();
}

Content scripts will still be run in non-incognito pages. To counter that, just exit your code after checking whether your extension is running in incognito mode (similar to the above check).

Note: If the extension is going to be published and only useful in incognito mode, consider checking whether incognito mode is enabled and offer instructions if it's disabled. See e.g. How can I enable my chrome extension in incognito mode?

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Hmm, that didn't work either. Can you check my updated question? This may be unrelated to it being in incognito mode. – Jack Rothrock Jul 04 '16 at 17:16
  • @JackRothrock Incognito is disabled by default. Did you enable incognito mode? – Rob W Jul 04 '16 at 19:13
  • Yeah. When I take it out of incognito - by removing the if - it seems to work by disabling cookies, but doesn't seem to change to user-agent headings. It works when doing individual sites, but not the http(s)?://*/* – Jack Rothrock Jul 04 '16 at 23:32
  • @JackRothrock How did you test it? The Network tab of the devtools does not show modifications by extensions. And note that changing the header does not change the value of `navigator.userAgent`. – Rob W Jul 04 '16 at 23:38
  • Oh, Interesting. Yeah, I was just using dev tool and just looking at the request headers while on localhost:3000 - which actually changes it for the req headers - as well as noticing the disabling of cookies on sites. Umm, soooo I don't know if know this already, but the internet is beautiful. I just found an old post of yours talking about [this](https://stackoverflow.com/questions/23202136/changing-navigator-useragent-using-chrome-extension), so thanks man haha. I'll dig deeper into this tomorrow – Jack Rothrock Jul 05 '16 at 06:50