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

@@ -5,86 +5,4 @@ Example Firefox add-ons created using the [WebExtensions](https://developer.mozi
* To test out an extension in Firefox, see [Packaging and Installation](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Packaging_and_installation).
## beastify ##
**Note that this example does not currently work in Firefox.**
An example to accompany a [walkthrough tutorial on MDN](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Walkthrough).
The extension includes:
* a browser action with a popup including HTML, CSS, and JS
* a content script
* three images, each of a different beast, packaged as web accessible resources
When the user clicks the browser action button, the popup is shown, enabling the user to choose one of three beasts.
When they choose, the extension injects the content script into the current page, and sends the content script a message containing the chosen beast.
When the content script receives this message, it replaces all <IMG> elements in the current page with the packaged image that corresponds to the chosen beast.
**What it shows**
* how to write a browser action with a popup
* how to give the popup style and behavior using CSS and JS
* how to inject a content script programmatically into a tab using tabs.executeScript()
* how to send a message from the main extension to a content script
* how to use web accessible resources to enable web pages to load packaged content
## borderify ##
This extension just includes:
* a content script, "borderify.js", that is injected into any pages under "mozilla.org/" or any of its subdomains
The content script draws a border around the document.body.
**What it shows**
* how to inject content scripts declaratively using manifest.json
## notify-link-clicks ##
This extension includes:
* a content script, "content-script.js", that is injected into any pages under "mozilla.org/" or any of its subdomains
* a background script, "background-script.js",
The content script listens for clicks in the page it's attached to. If a click is on a link, it messages the link's href to the background script.
The background script listens for this message. When it receives one, it displays a notification containing the href.
**What it shows**
* how to inject content scripts declaratively using manifest.json
* how to send messages from a content script to a background script
* how to display system notifications using the notifications API
## open-my-page ##
This extension includes:
* a background script, "background.js"
* a browser action
* a page "my-page.html"
All it does is: when the user clicks the button, open "my-page.html" in a new tab.
**What it shows**
* how to listen for browser action clicks in a background script
* how to open a page packaged with your extension
## page-to-extension-messaging ##
This extension includes:
* a content script, which is injected only into: "https://mdn.github.io/webextensions-examples/content-script-page-script-messaging.html"
The content script listens for messages from the same window posted using window.postMessage. When the content script receives such a message, it displays an alert.
To test it out, visit https://mdn.github.io/webextensions-examples/content-script-page-script-messaging.html and press the buttons. One button sends a message from the page script to the content script, the other button sends a message in the other direction.
**What it shows**
* how to send messages between page scripts and content scripts
* To see details of each extension, what it does and what it demonstrates, see the [WebExtensions Examples page](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples).

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
});
});