From 803383bea1237337a85837609701b04e2d3361d4 Mon Sep 17 00:00:00 2001 From: Youngsoo Choi Date: Wed, 20 Sep 2017 10:59:00 +0900 Subject: [PATCH] [WRT] Add chrome extension sample 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 --- .../chrome_extension_sample/background.js | 9 +++++++++ .../chrome_extension_sample/contentscript.js | 16 ++++++++++++++++ .../chrome_extension_sample/manifest.json | 21 +++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100755 wrt/wrt_support/extensions_repo/chrome_extension_sample/background.js create mode 100755 wrt/wrt_support/extensions_repo/chrome_extension_sample/contentscript.js create mode 100755 wrt/wrt_support/extensions_repo/chrome_extension_sample/manifest.json 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 index 0000000..fdc3e97 --- /dev/null +++ b/wrt/wrt_support/extensions_repo/chrome_extension_sample/background.js @@ -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 index 0000000..d060778 --- /dev/null +++ b/wrt/wrt_support/extensions_repo/chrome_extension_sample/contentscript.js @@ -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 index 0000000..5768145 --- /dev/null +++ b/wrt/wrt_support/extensions_repo/chrome_extension_sample/manifest.json @@ -0,0 +1,21 @@ +{ + "name": "chrome_extension_sample", + "description": "Alert", + "version": "2.0", + "background": { + "scripts": ["background.js"], + "persistent": false + }, + "content_scripts": [ + { + "matches": [ + "" + ], + "js": ["contentscript.js"] + } + ], + "browser_action": { + "default_title": "Alert" + }, + "manifest_version": 2 +} -- 2.7.4