[WRT] Add chrome extension sample 09/151109/5
authorYoungsoo Choi <kenshin.choi@samsung.com>
Wed, 20 Sep 2017 01:59:00 +0000 (10:59 +0900)
committermin7.choi <min7.choi@samsung.com>
Fri, 22 Sep 2017 01:43:49 +0000 (10:43 +0900)
This adds chrome extension sample for reference.

The extension has chrome extension style manifest.json that specifies
name, background, and contentscript.

It alerts popup when click event is emitted on web app.
Also, contentscript and background communicate each other via IPC
that can be checked by inspector console.

Change-Id: I92921b05fe07f9b0bb8e88d14d26e1bc3a59af6e
Signed-off-by: Youngsoo Choi <kenshin.choi@samsung.com>
wrt/wrt_support/extensions_repo/chrome_extension_sample/background.js [new file with mode: 0755]
wrt/wrt_support/extensions_repo/chrome_extension_sample/contentscript.js [new file with mode: 0755]
wrt/wrt_support/extensions_repo/chrome_extension_sample/manifest.json [new file with mode: 0755]

diff --git a/wrt/wrt_support/extensions_repo/chrome_extension_sample/background.js b/wrt/wrt_support/extensions_repo/chrome_extension_sample/background.js
new file mode 100755 (executable)
index 0000000..fdc3e97
--- /dev/null
@@ -0,0 +1,9 @@
+chrome.runtime.onConnect.addListener(function(port) {
+  console.assert(port.name == "contentscript");
+  port.onMessage.addListener(function(msg) {
+    if (msg.contentscript == "Hello background!") {
+      console.log('from contentscript : ' + msg.contentscript);
+      port.postMessage({background: "Hello contentscript!"});
+    }
+  });
+});
diff --git a/wrt/wrt_support/extensions_repo/chrome_extension_sample/contentscript.js b/wrt/wrt_support/extensions_repo/chrome_extension_sample/contentscript.js
new file mode 100755 (executable)
index 0000000..d060778
--- /dev/null
@@ -0,0 +1,16 @@
+var port = chrome.runtime.connect({name: "contentscript"});
+port.postMessage({contentscript: "Hello background!"});
+port.onMessage.addListener(function(msg) {
+  if (msg.background == "Hello contentscript!") {
+    console.log('from background : ' + msg.background);
+  }
+});
+
+var items = document.getElementsByTagName("*");
+for (var i = 0; i < items.length; i++) {
+  items[i].addEventListener("click", function(e) {
+    e.stopPropagation();
+    alert('hello world!');
+  });
+}
+
diff --git a/wrt/wrt_support/extensions_repo/chrome_extension_sample/manifest.json b/wrt/wrt_support/extensions_repo/chrome_extension_sample/manifest.json
new file mode 100755 (executable)
index 0000000..5768145
--- /dev/null
@@ -0,0 +1,21 @@
+{
+  "name": "chrome_extension_sample",
+  "description": "Alert",
+  "version": "2.0",
+  "background": {
+    "scripts": ["background.js"],
+    "persistent": false
+  },
+  "content_scripts": [
+    {
+      "matches": [
+        "<all_urls>"
+      ],
+      "js": ["contentscript.js"]
+    }
+  ],
+  "browser_action": {
+    "default_title": "Alert"
+  },
+  "manifest_version": 2
+}