[the-board/wip/http-api: 5/6] [chrome] Add initial code for The Board's chrome extension



commit 4ae622aab70def41b6ac1a7f63c272d3ed7ff586
Author: Lucas Rocha <lucasr gnome org>
Date:   Tue Nov 23 18:11:21 2010 +0000

    [chrome] Add initial code for The Board's chrome extension

 src/chrome/background.html |    1 +
 src/chrome/background.js   |  165 ++++++++++++++++++++++++++++++++++++++++++++
 src/chrome/icon-16.png     |  Bin 0 -> 353 bytes
 src/chrome/icon-48.png     |  Bin 0 -> 473 bytes
 src/chrome/manifest.json   |   15 ++++
 5 files changed, 181 insertions(+), 0 deletions(-)
---
diff --git a/src/chrome/background.html b/src/chrome/background.html
new file mode 100644
index 0000000..ac3f43e
--- /dev/null
+++ b/src/chrome/background.html
@@ -0,0 +1 @@
+<script src="background.js"></script>
diff --git a/src/chrome/background.js b/src/chrome/background.js
new file mode 100644
index 0000000..1d42ce7
--- /dev/null
+++ b/src/chrome/background.js
@@ -0,0 +1,165 @@
+var _THE_BOARD_URL = "http://localhost:2010/";;
+
+function callMethod(methodName, args, onLoad, onError) {
+    var req = new XMLHttpRequest();
+
+    var url = _THE_BOARD_URL + methodName;
+
+    if (args) {
+        url += "?";
+
+        var first = true;
+        for (argName in args) {
+            url += (first ? "" : "&") +
+                   argName + "=" + args[argName];
+
+            first = false;
+        }
+    }
+
+    req.open("POST", encodeURI(url), true);
+
+    var boundaryString = "AaBbCcX30";
+    var boundary = "--"+boundaryString;
+
+    var postContent = "\r\n"+boundary+"\r\n"+
+            "Content-Disposition: form-data; name=\"comment\"\r\n"+
+            "\r\n"+
+            "Comment is another input\r\n"+
+            boundary+"\r\n"+
+            "Content-Disposition: file; name=\"uploadedfile\"; filename=\"image.png\"\r\n"+
+            "Content-Type: image/png\r\n"+
+            "\r\n"+
+            "%FILECONTENT%\r\n"+
+            boundary+"\r\n";
+
+    postContent = postContent.replace("%FILECONTENT%", args.imageData);
+
+    req.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundaryString);
+
+    var onStateChange = function() {
+        if (req.readyState == 4) {
+            var json;
+
+            if (req.responseText) {
+                json = JSON.parse(req.responseText);
+            } else {
+                json = {};
+            }
+
+            onLoad(json);
+        }
+    }
+
+    var onErrorInternal = function() {
+        onError();
+    }
+
+    if (onLoad) {
+        req.onreadystatechange = onStateChange;
+    }
+
+    if (onError) {
+        req.onerror = onErrorInternal;
+    }
+
+    req.send(postContent);
+}
+
+var errorNotification = null;
+
+function closeErrorNofification() {
+    if (!errorNotification) {
+        return;
+    }
+
+    errorNotification.cancel();
+    errorNotification = null;
+}
+
+function showErrorNotification() {
+    if (errorNotification) {
+        return;
+    }
+
+    errorNotification =
+        webkitNotifications.createNotification('icon-48.png',
+                                               'The Board is not running',
+                                               'The Board must be running before ' +
+                                               'you start adding content to it.');
+
+    errorNotification.show();
+
+    setTimeout(closeErrorNofification, 3000);
+}
+
+function addPhotoToTheBoard(imageUrl) {
+    var args = { thingId: "photo" };
+
+    var imageReq = new XMLHttpRequest();
+
+    imageReq.open("GET", encodeURI(imageUrl), true);
+
+    var onImageStateChange = function() {
+        if (imageReq.readyState == 4) {
+            if (imageReq.responseText) {
+                args.imageData = imageReq.responseText;
+
+                callMethod("addThing", args,
+                           null, showErrorNotification);
+            }
+        }
+    }
+
+    var onImageError = function() {
+        // FIXME: handle errors here. Show a notification?
+    }
+
+    imageReq.onreadystatechange = onImageStateChange;
+    imageReq.onerror = onImageError;
+
+    imageReq.send(null);
+}
+
+function addNoteToTheBoard(text) {
+    var args = { thingId: "note",
+                 text: text };
+
+    callMethod("addThing", args,
+               null, showErrorNotification);
+}
+
+function addLabelToTheBoard(text) {
+    var args = { thingId: "label",
+                 text: text };
+
+    callMethod("addThing", args,
+               null, showErrorNotification);
+}
+
+function onMenuClick(info, tab) {
+    console.log(info);
+
+    if ('srcUrl' in info) {
+        addPhotoToTheBoard(info.srcUrl);
+    } else if ('linkUrl' in info) {
+        addLabelToTheBoard(info.linkUrl);
+    } else if ('selectionText' in info) {
+        addNoteToTheBoard(info.selectionText);
+    }
+}
+
+var contextMenuItemId = null;
+
+function addContextMenuItem() {
+    if (contextMenuItemId) {
+        return;
+    }
+
+    contextMenuItemId =
+        chrome.contextMenus.create({ title: "Add to The Board",
+                                     contexts: ["selection", "link", "image"],
+                                     onclick: onMenuClick });
+}
+
+addContextMenuItem();
diff --git a/src/chrome/icon-16.png b/src/chrome/icon-16.png
new file mode 100644
index 0000000..265231f
Binary files /dev/null and b/src/chrome/icon-16.png differ
diff --git a/src/chrome/icon-48.png b/src/chrome/icon-48.png
new file mode 100644
index 0000000..53ff199
Binary files /dev/null and b/src/chrome/icon-48.png differ
diff --git a/src/chrome/manifest.json b/src/chrome/manifest.json
new file mode 100644
index 0000000..ec0c390
--- /dev/null
+++ b/src/chrome/manifest.json
@@ -0,0 +1,15 @@
+{
+  "name": "The Board",
+  "version": "0.1",
+  "description": "Chrome's integration with GNOME's The Board",
+  "background_page": "background.html",
+  "permissions": [
+    "contextMenus",
+    "notifications",
+    "<all_urls>"
+  ],
+  "icons": {
+    "16": "icon-16.png",
+    "48": "icon-48.png"
+  }
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]