Add alt for image

diff --git a/src/md-linker.js b/src/md-linker.js
index 18d78ab..9ec4847 100644
--- a/src/md-linker.js
+++ b/src/md-linker.js
@@ -1,3 +1,6 @@
+var REQUEST = null;
+var CONTEXT = null;
+
 function copy(text) {
   var clip = document.getElementById("clipboard_area");
   clip.value = text;
@@ -5,31 +8,44 @@ function copy(text) {
   document.execCommand("copy");
 }

-function genericOnClick(info, tab) {
-  console.log(info);
+(function () {
+  var props = {
+    //"title": chrome.i18n.getMessage('title'),
+    "title": "Copy link with markdown-format",
+    "contexts": ["page","link","image","video","audio"],
+    "onclick": function (info) {
+      console.log(info);

-  if(info.srcUrl != null){
-    // image
-    // TODO get alt text
-    copy("![" + "alt" + "](" + info.srcUrl + ")");
+      if(info.srcUrl != null){
+        // image
+        // TODO get alt text
+        copy("![" + "alt" + "](" + info.srcUrl + ")");

-  }else if(info.linkUrl != null){
-    // text link
-    var text = info.selectionText || ""
-    copy("[" + text + "](" + info.linkUrl + ")");
+      }else if(info.linkUrl != null){
+        // text link
+        var text = info.selectionText || ""
+        copy("[" + text + "](" + info.linkUrl + ")");

-  }else{
-    // page
-    chrome.tabs.getSelected(null, function(tab){
-      copy("[" + tab.title + "](" + info.pageUrl + ")")
-    });
+      }else{
+        // page
+        chrome.tabs.getSelected(null, function(tab){
+          copy("[" + tab.title + "](" + info.pageUrl + ")")
+        });

+      }
+    }
+    if (CONTEXT){
+      chrome.contextMenus.update(CONTEXT, props);
+    }else{
+      CONTEXT = chrome.contextMenus.create(props);
+    }
   }
-}
-
-var context = ["page","link","image","video",
-                "audio"];
-var title = "Copy link with markdown-format";
-var id = chrome.contextMenus.create({"title": title, "contexts":context,
-                                       "onclick": genericOnClick});
+}());

+chrome.extension.onRequest.addListener(
+    function(request, sender, sendResponse) {
+        console.log(request);
+        console.log(sender);
+        REQUEST = request;
+    }
+);
\ No newline at end of file

[from now] 2012/07/23 23:50:48

diff --git a/src/manifest.json b/src/manifest.json
index 6cd9893..3d3ec08 100644
--- a/src/manifest.json
+++ b/src/manifest.json
@@ -8,5 +8,9 @@
   "background": {
     "page": "background.html"
   },
+  "content_scripts": [{
+    "matches": ["<all_urls>"],
+    "js": ["content_script.js"]
+  }],
   "permissions": ["contextMenus", "tabs"]
 }
\ No newline at end of file
diff --git a/src/md-linker.js b/src/md-linker.js
index 9ec4847..ffd61c8 100644
--- a/src/md-linker.js
+++ b/src/md-linker.js
@@ -13,7 +13,7 @@ function copy(text) {
     //"title": chrome.i18n.getMessage('title'),
     "title": "Copy link with markdown-format",
     "contexts": ["page","link","image","video","audio"],
-    "onclick": function (info) {
+    "onclick": function (info, tab) {
       console.log(info);

       if(info.srcUrl != null){
@@ -31,15 +31,14 @@ function copy(text) {
         chrome.tabs.getSelected(null, function(tab){
           copy("[" + tab.title + "](" + info.pageUrl + ")")
         });
-
       }
     }
-    if (CONTEXT){
-      chrome.contextMenus.update(CONTEXT, props);
-    }else{
-      CONTEXT = chrome.contextMenus.create(props);
-    }
   }
+  if(CONTEXT){
+        chrome.contextMenus.update(CONTEXT, props);
+      }else{
+        CONTEXT = chrome.contextMenus.create(props);
+      }
 }());

 chrome.extension.onRequest.addListener(

[from now] 2012/07/23 23:51:01

diff --git a/src/content_script.js b/src/content_script.js
new file mode 100644
index 0000000..1c20f99
--- /dev/null
+++ b/src/content_script.js
@@ -0,0 +1,11 @@
+document.addEventListener("contextmenu", function (e) {
+    var elem = e.srcElement;
+    if (elem instanceof HTMLImageElement) {
+        chrome.extension.sendRequest({
+          alt: elem.alt,
+          src: elem.src,
+          width: elem.width,
+          height: elem.height
+        });
+    }
+}, true);
\ No newline at end of file

[from now] 2012/07/24 00:01:40

diff --git a/src/manifest.json b/src/manifest.json
index 3d3ec08..a2abcf2 100644
--- a/src/manifest.json
+++ b/src/manifest.json
@@ -1,6 +1,6 @@
 {
   "name": "Markdown Linker",
-  "version": "0.1.2",
+  "version": "0.1.3",
   "manifest_version": 2,
   "description": "Make it easy to copy links with markdown-format",
   "icons": { "48": "icon/icon_048.png",
diff --git a/src/md-linker.js b/src/md-linker.js
index ffd61c8..a1dda61 100644
--- a/src/md-linker.js
+++ b/src/md-linker.js
@@ -12,14 +12,17 @@ function copy(text) {
   var props = {
     //"title": chrome.i18n.getMessage('title'),
     "title": "Copy link with markdown-format",
-    "contexts": ["page","link","image","video","audio"],
+    "contexts": ["image", "link", "page"],
     "onclick": function (info, tab) {
-      console.log(info);
+      var alt = "";
+      if(REQUEST != null){
+        alt = REQUEST.alt || "";
+        REQUEST = null;
+      }

       if(info.srcUrl != null){
         // image
-        // TODO get alt text
-        copy("![" + "alt" + "](" + info.srcUrl + ")");
+        copy("![" + alt + "](" + info.srcUrl + ")");

       }else if(info.linkUrl != null){
         // text link
@@ -35,16 +38,14 @@ function copy(text) {
     }
   }
   if(CONTEXT){
-        chrome.contextMenus.update(CONTEXT, props);
-      }else{
-        CONTEXT = chrome.contextMenus.create(props);
-      }
+    chrome.contextMenus.update(CONTEXT, props);
+  }else{
+    CONTEXT = chrome.contextMenus.create(props);
+  }
 }());

 chrome.extension.onRequest.addListener(
     function(request, sender, sendResponse) {
-        console.log(request);
-        console.log(sender);
         REQUEST = request;
     }
 );
\ No newline at end of file