[tor-commits] [flashproxy/master] Add flashproxy.js.
dcf at torproject.org
dcf at torproject.org
Mon Oct 17 02:28:24 UTC 2011
commit 038c8f7c215ea1fcf32c61cd76f0ff80f4bb7990
Author: David Fifield <david at bamsoftware.com>
Date: Sun Oct 16 19:12:11 2011 -0700
Add flashproxy.js.
This is a file that will be hosted at crypto.stanford.edu to make it
easy to install the badge on a web page.
This script also does the User-Agent detection (to politely disable
itself for cell phones) that was formerly done by swfcat itself.
---
README | 12 +++---
flashproxy.js | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 147 insertions(+), 6 deletions(-)
diff --git a/README b/README
index 1a49686..6db4ee0 100644
--- a/README
+++ b/README
@@ -150,13 +150,13 @@ wait a few minutes. It can take a while to download relay descriptors.
== How to put a flash proxy badge on a web page.
-Copy swfcat.swf to your server, then paste in this HTML:
+Copy swfcat.swf to your server, then paste in this HTML where you want
+the badge to appear:
+
+<script type="text/javascript" src="https://crypto.stanford.edu/flashproxy/flashproxy.js"></script>
+
+It will be inserted as a span element with the id "flashproxy-badge".
-<object width="70" height="23">
- <param name="movie" value="swfcat.swf">
- <param name="flashvars" value="">
- <embed src="swfcat.swf" width="70" height="23" flashvars=""></embed>
-</object>
== For developers
diff --git a/flashproxy.js b/flashproxy.js
new file mode 100644
index 0000000..fea72f9
--- /dev/null
+++ b/flashproxy.js
@@ -0,0 +1,141 @@
+/* Are circumstances such that we should self-disable and not be a
+ proxy? We take a best-effort guess as to whether this device runs on
+ a battery or the data transfer might be expensive.
+
+ Matching mobile User-Agents is complex; but we only need to match
+ those devices that can also run a recent version of Adobe Flash,
+ which is a subset of this list:
+ https://secure.wikimedia.org/wikipedia/en/wiki/Adobe_Flash_Player#Mobile_operating_systems
+
+ Other resources:
+ http://www.zytrax.com/tech/web/mobile_ids.html
+ http://googlewebmastercentral.blogspot.com/2011/03/mo-better-to-also-detect-mobile-user.html
+ http://search.cpan.org/~cmanley/Mobile-UserAgent-1.05/lib/Mobile/UserAgent.pm
+*/
+function flashproxy_should_disable()
+{
+ var ua;
+
+ ua = window.navigator.userAgent;
+ if (ua != null) {
+ const UA_LIST = [
+ /\bmobile\b/i,
+ /\bandroid\b/i,
+ /\bopera mobi\b/i,
+ ];
+
+ for (var i = 0; i < UA_LIST.length; i++) {
+ var re = UA_LIST[i];
+
+ if (ua.match(re)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+/* Create and return a DOM fragment:
+<span id=BADGE_ID>
+<a href=FLASHPROXY_INFO_URL>
+ child
+</a>
+</span>
+*/
+function flashproxy_make_container(child)
+{
+ const BADGE_ID = "flashproxy-badge";
+ const FLASHPROXY_INFO_URL = "https://crypto.stanford.edu/flashproxy/";
+
+ var container;
+ var a;
+
+ container = document.createElement("span");
+ container.setAttribute("id", "flashproxy-badge");
+ a = document.createElement("a");
+ a.setAttribute("href", FLASHPROXY_INFO_URL);
+ a.appendChild(child)
+ container.appendChild(a);
+
+ return container;
+}
+
+/* Create and return a DOM fragment:
+<object width=WIDTH height=HEIGHT>
+ <param name="movie" value=SWFCAT_URL>
+ <param name="flashvars" value=FLASHVARS>
+ <embed src=SWFCAT_URL width=WIDTH height=HEIGHT flashvars=FLASHVARS></embed>
+</object>
+*/
+function flashproxy_make_badge()
+{
+ const WIDTH = 70;
+ const HEIGHT = 23;
+ const FLASHVARS = "";
+ const SWFCAT_URL = "https://crypto.stanford.edu/flashproxy/swfcat.swf";
+
+ var object;
+ var param;
+ var embed;
+
+ object = document.createElement("object");
+ object.setAttribute("width", WIDTH);
+ object.setAttribute("height", HEIGHT);
+
+ param = document.createElement("param");
+ param.setAttribute("name", "movie");
+ param.setAttribute("value", SWFCAT_URL);
+ object.appendChild(param);
+ param = document.createElement("param");
+ param.setAttribute("name", "flashvars");
+ param.setAttribute("value", FLASHVARS);
+ object.appendChild(param);
+
+ embed = document.createElement("embed");
+ embed.setAttribute("src", SWFCAT_URL);
+ embed.setAttribute("width", WIDTH);
+ embed.setAttribute("height", HEIGHT);
+ embed.setAttribute("flashvars", FLASHVARS);
+ object.appendChild(embed);
+
+ return object;
+}
+
+/* Create and return a non-functional placeholder badge DOM fragment:
+ <img src=BADGE_IMAGE_URL border="0">
+*/
+function flashproxy_make_dummy_badge()
+{
+ const BADGE_IMAGE_URL = "https://crypto.stanford.edu/flashproxy/badge.png";
+
+ var img;
+
+ img = document.createElement("img");
+ img.setAttribute("src", BADGE_IMAGE_URL);
+ img.setAttribute("border", 0);
+
+ return img;
+}
+
+function flashproxy_badge_insert()
+{
+ var badge;
+ var e;
+
+ if (flashproxy_should_disable()) {
+ badge = flashproxy_make_dummy_badge();
+ } else {
+ badge = flashproxy_make_badge();
+ }
+
+ /* http://intertwingly.net/blog/2006/11/10/Thats-Not-Write for this trick to
+ insert right after the <script> element in the DOM. */
+ e = document;
+ while (e.lastChild && e.lastChild.nodeType == 1) {
+ e = e.lastChild;
+ }
+ e.parentNode.appendChild(flashproxy_make_container(badge));
+}
+
+flashproxy_badge_insert();
More information about the tor-commits
mailing list