[the-board] [chrome] Initial code for The Board's chrome extension
- From: Lucas Rocha <lucasr src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [the-board] [chrome] Initial code for The Board's chrome extension
- Date: Sun, 28 Nov 2010 12:35:32 +0000 (UTC)
commit cb3b3f6a7050e6cdb3b5209054509d49e610a869
Author: Lucas Rocha <lucasr gnome org>
Date: Tue Nov 23 18:11:21 2010 +0000
[chrome] Initial code for The Board's chrome extension
For now, it only allows adding label and notes to The Board.
src/chrome/background.html | 1 +
src/chrome/background.js | 176 ++++++++++++++++++++++++++++++++++++++++++++
src/chrome/icon-16.png | Bin 0 -> 353 bytes
src/chrome/icon-48.png | Bin 0 -> 473 bytes
src/chrome/manifest.json | 16 ++++
5 files changed, 193 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..12244d6
--- /dev/null
+++ b/src/chrome/background.js
@@ -0,0 +1,176 @@
+var _THE_BOARD_URL = "http://localhost:2010/";
+
+function callMethod(methodName, args, onLoad, onError) {
+ var req = new XMLHttpRequest();
+
+ var url = _THE_BOARD_URL + methodName;
+
+ var fileData = args.fileData;
+ delete args.fileData;
+
+ if (args) {
+ url += "?";
+
+ var first = true;
+ for (argName in args) {
+ url += (first ? "" : "&") +
+ argName + "=" + args[argName];
+
+ first = false;
+ }
+ }
+
+ req.open("POST", encodeURI(url), true);
+
+ var postContent = null;
+
+ if (fileData) {
+ var boundaryString = "AaBbCcX30";
+ var boundary = "--" + boundaryString;
+
+ req.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundaryString);
+
+ postContent =
+ boundary + "\r\n" +
+ "Content-Disposition: form-data; name=\"file\"; filename=\"uploaded-file\"\r\n" +
+ "Content-Type: application/octet-stream\r\n" +
+ "\r\n" +
+ fileData + "\r\n" +
+ boundary+"--\r\n";
+ }
+
+ var onStateChange = function() {
+ if (req.readyState == 4 && req.status == 200) {
+ 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 = { id: "photo" };
+
+ var imageReq = new XMLHttpRequest();
+
+ imageReq.open("GET", imageUrl, true);
+ imageReq.overrideMimeType("text/plain; charset=x-user-defined");
+
+ var onImageStateChange = function() {
+ if (imageReq.readyState == 4 && imageReq.status == 200) {
+ if (imageReq.responseText) {
+ args.fileData = "";
+
+ // Remove the text-related bits from image data
+ for (var i=0; i <= imageReq.responseText.length - 1; i++) {
+ args.fileData +=
+ String.fromCharCode(imageReq.responseText.charCodeAt(i) & 0xff);
+ }
+
+ 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 = { id: "note",
+ text: text };
+
+ callMethod("addThing", args,
+ null, showErrorNotification);
+}
+
+function addLabelToTheBoard(text) {
+ var args = { id: "label",
+ text: text };
+
+ callMethod("addThing", args,
+ null, showErrorNotification);
+}
+
+function onMenuClick(info, tab) {
+ // FIXME: Chrome doesn't handle binary uploads well
+ // at the moment, so we're not doing anything on images.
+ // Once Chrome starts working, enable "image" context
+ // when creating the context menu and call addPhotoToTheBoard
+ // when srcUrl is defined here.
+
+ 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"],
+ 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..4cd4f43
--- /dev/null
+++ b/src/chrome/manifest.json
@@ -0,0 +1,16 @@
+{
+ "name": "The Board",
+ "version": "0.1",
+ "description": "Chrome's integration with GNOME's The Board",
+ "background_page": "background.html",
+ "permissions": [
+ "contextMenus",
+ "notifications",
+ "http://*/*",
+ "https://*/*"
+ ],
+ "icons": {
+ "16": "icon-16.png",
+ "48": "icon-48.png"
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]