summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions
diff options
context:
space:
mode:
authorasargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-21 23:02:06 +0000
committerasargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-07-21 23:02:06 +0000
commit8d010f670b2b7a655ee1c72c6ef79c6bd1cca823 (patch)
treeb32f7c8851883dcc91c9d395c205ff7d989fd9bb /chrome/common/extensions
parent55c468f4a50d5c3568818d761c67e61181731445 (diff)
downloadchromium_src-8d010f670b2b7a655ee1c72c6ef79c6bd1cca823.zip
chromium_src-8d010f670b2b7a655ee1c72c6ef79c6bd1cca823.tar.gz
chromium_src-8d010f670b2b7a655ee1c72c6ef79c6bd1cca823.tar.bz2
Adding some sample code showing usage of contextMenu API.
BUG=49687 TEST=none TBR=rafaelw git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53258 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions')
-rw-r--r--chrome/common/extensions/docs/examples/api/contextMenus/basic/background.html1
-rw-r--r--chrome/common/extensions/docs/examples/api/contextMenus/basic/manifest.json7
-rw-r--r--chrome/common/extensions/docs/examples/api/contextMenus/basic/sample.js69
3 files changed, 77 insertions, 0 deletions
diff --git a/chrome/common/extensions/docs/examples/api/contextMenus/basic/background.html b/chrome/common/extensions/docs/examples/api/contextMenus/basic/background.html
new file mode 100644
index 0000000..8fc4efe
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/contextMenus/basic/background.html
@@ -0,0 +1 @@
+<script src="sample.js"></script>
diff --git a/chrome/common/extensions/docs/examples/api/contextMenus/basic/manifest.json b/chrome/common/extensions/docs/examples/api/contextMenus/basic/manifest.json
new file mode 100644
index 0000000..bc6fb3f6
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/contextMenus/basic/manifest.json
@@ -0,0 +1,7 @@
+{
+ "name": "Context Menus Sample",
+ "description": "Shows some of the features of the Context Menus API",
+ "version": "0.5",
+ "permissions": ["contextMenus"],
+ "background_page": "background.html"
+}
diff --git a/chrome/common/extensions/docs/examples/api/contextMenus/basic/sample.js b/chrome/common/extensions/docs/examples/api/contextMenus/basic/sample.js
new file mode 100644
index 0000000..a58be3a
--- /dev/null
+++ b/chrome/common/extensions/docs/examples/api/contextMenus/basic/sample.js
@@ -0,0 +1,69 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// A generic onclick callback function.
+function genericOnClick(info, tab) {
+ console.log("item " + info.menuItemId + " was clicked");
+ console.log("info: " + JSON.stringify(info));
+ console.log("tab: " + JSON.stringify(tab));
+}
+
+// Create one test item for each context type.
+var contexts = ["page","selection","link","editable","image","video",
+ "audio"];
+for (var i = 0; i < contexts.length; i++) {
+ var context = contexts[i];
+ var title = "Test '" + context + "' menu item";
+ var id = chrome.contextMenus.create({"title": title, "contexts":[context],
+ "onclick": genericOnClick});
+ console.log("'" + context + "' item:" + id);
+}
+
+
+// Create a parent item and two children.
+var parent = chrome.contextMenus.create({"title": "Test parent item"});
+var child1 = chrome.contextMenus.create(
+ {"title": "Child 1", "parentId": parent, "onclick": genericOnClick});
+var child2 = chrome.contextMenus.create(
+ {"title": "Child 2", "parentId": parent, "onclick": genericOnClick});
+console.log("parent:" + parent + " child1:" + child1 + " child2:" + child2);
+
+
+// Create some radio items.
+function radioOnClick(info, tab) {
+ console.log("radio item " + info.menuItemId +
+ " was clicked (previous checked state was " +
+ info.wasChecked + ")");
+}
+var radio1 = chrome.contextMenus.create({"title": "Radio 1", "type": "radio",
+ "onclick":radioOnClick});
+var radio2 = chrome.contextMenus.create({"title": "Radio 2", "type": "radio",
+ "onclick":radioOnClick});
+console.log("radio1:" + radio1 + " radio2:" + radio2);
+
+
+// Create some checkbox items.
+function checkboxOnClick(info, tab) {
+ console.log(JSON.stringify(info));
+ console.log("checkbox item " + info.menuItemId +
+ " was clicked, state is now: " + info.checked +
+ "(previous state was " + info.wasChecked + ")");
+
+}
+var checkbox1 = chrome.contextMenus.create(
+ {"title": "Checkbox1", "type": "checkbox", "onclick":checkboxOnClick});
+var checkbox2 = chrome.contextMenus.create(
+ {"title": "Checkbox2", "type": "checkbox", "onclick":checkboxOnClick});
+console.log("checkbox1:" + checkbox1 + " checkbox2:" + checkbox2);
+
+
+// Intentionally create an invalid item, to show off error checking in the
+// create callback.
+console.log("About to try creating an invalid item - an error about " +
+ "item 999 should show up");
+chrome.contextMenus.create({"title": "Oops", "parentId":999}, function() {
+ if (chrome.extension.lastError) {
+ console.log("Got expected error: " + chrome.extension.lastError.message);
+ }
+});