added user-agent-rewriter, updated README

This commit is contained in:
Will Bamberg
2015-10-05 22:26:56 -07:00
parent b68cb3e304
commit ddcc1dc11d
8 changed files with 107 additions and 83 deletions

View File

@@ -0,0 +1,30 @@
"use strict";
var targetPage = "http://useragentstring.com/*";
var uaStrings = {
"Firefox 41": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0",
"Chrome 41": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
"IE 11": "Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko"
}
var ua = uaStrings["Firefox 41"];
chrome.webRequest.onBeforeSendHeaders.addListener(rewriteUserAgentHeader,
{urls: [targetPage]},
["blocking", "requestHeaders"]);
function rewriteUserAgentHeader(e) {
for (var header of e.requestHeaders) {
if (header.name == "User-Agent") {
header.value = ua;
}
}
return {requestHeaders: e.requestHeaders};
}
chrome.runtime.onMessage.addListener(setUaString);
function setUaString(message) {
ua = uaStrings[message.uaString];
}

View File

@@ -0,0 +1 @@
The icon “choose_ua.png” is taken from Yummygums Iconsweets iconset, and is used under the terms of its license (http://yummygum.com/work/iconsweets).

Binary file not shown.

After

Width:  |  Height:  |  Size: 471 B

View File

@@ -0,0 +1,27 @@
{
"manifest_version": 2,
"name": "user-agent-rewriter",
"version": "1.0",
"applications": {
"gecko": {
"id": "user-agent-rewriter@mozilla.org"
}
},
"permissions": [
"webRequest", "webRequestBlocking", "http://useragentstring.com/*"
],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "button/choose_ua.png",
"default_title": "Choose a user agent",
"default_popup": "popup/choose_ua.html"
}
}

View File

@@ -0,0 +1,18 @@
html, body {
height: 100px;
width: 100px;
margin: 0;
}
.ua-choice {
height: 20%;
width: 90%;
margin: 3% auto;
padding: 8% 6% 0 6%;
background-color: #E5F2F2;
cursor: pointer;
}
.ua-choice:hover {
background-color: #CFF2F2;
}

View File

@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="choose_ua.css"/>
</head>
<body>
<div class="ua-choice">Firefox 41</div>
<div class="ua-choice">Chrome 41</div>
<div class="ua-choice">IE 11</div>
<script src="choose_ua.js"></script>
</body>
</html>

View File

@@ -0,0 +1,12 @@
document.addEventListener("click", function(e) {
if (!e.target.classList.contains("ua-choice")) {
return;
}
var chosenUa = e.target.textContent;
chrome.runtime.sendMessage({
"command": "set-user-agent",
"uaString": chosenUa
});
});