diff --git a/README.md b/README.md index a1232db..b95f5c5 100644 --- a/README.md +++ b/README.md @@ -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 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). diff --git a/user-agent-rewriter/background.js b/user-agent-rewriter/background.js new file mode 100644 index 0000000..545d35a --- /dev/null +++ b/user-agent-rewriter/background.js @@ -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]; +} diff --git a/user-agent-rewriter/button/LICENSE b/user-agent-rewriter/button/LICENSE new file mode 100644 index 0000000..5a4c0a5 --- /dev/null +++ b/user-agent-rewriter/button/LICENSE @@ -0,0 +1 @@ +The icon “choose_ua.png” is taken from Yummygum’s Iconsweets iconset, and is used under the terms of its license (http://yummygum.com/work/iconsweets). diff --git a/user-agent-rewriter/button/choose_ua.png b/user-agent-rewriter/button/choose_ua.png new file mode 100644 index 0000000..08b9108 Binary files /dev/null and b/user-agent-rewriter/button/choose_ua.png differ diff --git a/user-agent-rewriter/manifest.json b/user-agent-rewriter/manifest.json new file mode 100644 index 0000000..29c54d0 --- /dev/null +++ b/user-agent-rewriter/manifest.json @@ -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" + } + +} diff --git a/user-agent-rewriter/popup/choose_ua.css b/user-agent-rewriter/popup/choose_ua.css new file mode 100644 index 0000000..2815fd5 --- /dev/null +++ b/user-agent-rewriter/popup/choose_ua.css @@ -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; +} diff --git a/user-agent-rewriter/popup/choose_ua.html b/user-agent-rewriter/popup/choose_ua.html new file mode 100644 index 0000000..6f5cee3 --- /dev/null +++ b/user-agent-rewriter/popup/choose_ua.html @@ -0,0 +1,18 @@ + + + + + + + + + + +
Firefox 41
+
Chrome 41
+
IE 11
+ + + + + diff --git a/user-agent-rewriter/popup/choose_ua.js b/user-agent-rewriter/popup/choose_ua.js new file mode 100644 index 0000000..f7d4f88 --- /dev/null +++ b/user-agent-rewriter/popup/choose_ua.js @@ -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 + }); +});