Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added base source code for tutorial

  • Loading branch information...
commit 5734a4f34e4f00c982fcb63b847f443031c5b2d5 0 parents
@ZER0 authored
5 README.md
@@ -0,0 +1,5 @@
+This is the penny-arcade-comics add-on. It contains:
+
+* A program (lib/main.js).
+* A few tests.
+* Some meager documentation.
24 data/content.css
@@ -0,0 +1,24 @@
+#penny-arcade-addon-comics {
+ position: fixed;
+ top: 50%;
+ left : 50%;
+ width: 800px;
+ height: 400px;
+ z-index: 999;
+ margin: -200px 0 0 -400px;
+ background-color: rgba(0, 0, 0, 0.75);
+ background-repeat: no-repeat;
+ box-shadow: 0 0 15px 5px #000;
+}
+
+@media (orientation:portrait) {
+ #penny-arcade-addon-comics {
+ display: none;
+ }
+}
+
+@media (orientation:landscape) {
+ #penny-arcade-addon-comics {
+ display: block;
+ }
+}
31 data/content.js
@@ -0,0 +1,31 @@
+function addStyleSheet(url) {
+ var link = document.createElement("link");
+
+ link.setAttribute("href", url);
+ link.setAttribute("type", "text/css");
+ link.setAttribute("rel", "StyleSheet");
+
+ document.documentElement.appendChild(link);
+}
+
+self.port.on("comics-url", function(url) {
+ var comicsImage = document.createElement("div");
+
+ comicsImage.id = "penny-arcade-addon-comics";
+ comicsImage.style.backgroundImage = "url(" + url + ")";
+
+ document.body.appendChild(comicsImage);
+});
+
+self.port.on("init", function init(cssURL) {
+ // We're not interested in frames
+ if (window.frameElement) return;
+
+ console.log("content script, init");
+
+ addStyleSheet(cssURL);
+
+ var comicsPageURL = document.querySelector(".btnComic").href;
+
+ self.port.emit("initialized", comicsPageURL);
+});
2  doc/main.md
@@ -0,0 +1,2 @@
+The main module is a program that creates a widget. When a user clicks on
+the widget, the program loads the mozilla.org website in a new tab.
39 lib/main.js
@@ -0,0 +1,39 @@
+var PageMod = require("page-mod").PageMod;
+var data = require("self").data;
+var Request = require("request").Request;
+
+PageMod({
+ // page-mod will added only on the homepage and news page of penny-arcade.com,
+ // with or without www as prefix.
+ include: /^http:\/\/(www\.)?penny-arcade.com(\/|\/\d{4})?.*/,
+
+ contentScriptFile: data.url("content.js"),
+
+ contentScriptWhen: "ready",
+
+ onAttach: function (worker) {
+ worker.port.on("initialized", function(comicsPageURL) {
+ console.log("initialized, comics page URL:", comicsPageURL);
+
+ Request({
+ url: comicsPageURL,
+
+ onComplete: function (response) {
+ let imageMatches = response.text.match(/<img src="([^"]+)"/i);
+
+ if (imageMatches === null) {
+ console.exception("Something went wrong")
+ return;
+ }
+
+ console.log("comics image URL:", imageMatches[1]);
+
+ worker.port.emit("comics-url", imageMatches[1]);
+
+ }
+ }).get();
+ });
+
+ worker.port.emit("init", data.url("content.css"));
+ }
+});
9 package.json
@@ -0,0 +1,9 @@
+{
+ "name": "penny-arcade-comics",
+ "license": "MPL 1.1/GPL 2.0/LGPL 2.1",
+ "author": "",
+ "version": "0.1",
+ "fullName": "penny-arcade-comics",
+ "id": "jid1-OgdUHHYkPsrnuQ",
+ "description": "a basic add-on"
+}
32 test/test-main.js
@@ -0,0 +1,32 @@
+const main = require("main");
+
+exports.test_test_run = function(test) {
+ test.pass("Unit test running!");
+};
+
+exports.test_id = function(test) {
+ test.assert(require("self").id.length > 0);
+};
+
+exports.test_url = function(test) {
+ require("request").Request({
+ url: "http://www.mozilla.org/",
+ onComplete: function(response) {
+ test.assertEqual(response.statusText, "OK");
+ test.done();
+ }
+ }).get();
+ test.waitUntilDone(20000);
+};
+
+exports.test_open_tab = function(test) {
+ const tabs = require("tabs");
+ tabs.open({
+ url: "http://www.mozilla.org/",
+ onReady: function(tab) {
+ test.assertEqual(tab.url, "http://www.mozilla.org/");
+ test.done();
+ }
+ });
+ test.waitUntilDone(20000);
+};
Please sign in to comment.
Something went wrong with that request. Please try again.