Added Basic contextMenus sample (#859)

* Added Basic contextMenus sample

* Updated license header

* Update sample.js

* Update api-samples/contextMenus/basic/sample.js

Co-authored-by: Oliver Dunk <oliverdunk@google.com>

* Update api-samples/contextMenus/basic/sample.js

Co-authored-by: Oliver Dunk <oliverdunk@google.com>

* Update sample.js

* Update sample.js

* Create README.md

* Update api-samples/contextMenus/basic/README.md

Co-authored-by: Oliver Dunk <oliverdunk@google.com>

---------

Co-authored-by: Oliver Dunk <oliverdunk@google.com>
This commit is contained in:
IanStanion-google
2023-04-28 09:58:57 -04:00
committed by GitHub
parent 7f9f5b8482
commit 6b334ca8d1
3 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
# chrome.contextMenus
A sample extension demonstrating use of the chrome.contextMenus API.
## Overview
In this sample the chrome.contextMenus API is used to create menu items, listen for different context menu types being clicked, and run code based on what the user has selected.
## Implementation Notes
Different console readouts are made when context menu items are clicked. This can be quickly adapted to use new functions or API calls for more advaced functionality.

View File

@@ -0,0 +1,10 @@
{
"name": "Context Menus Sample",
"description": "Shows some of the features of the Context Menus API",
"version": "0.7",
"permissions": ["contextMenus"],
"background": {
"service_worker": "sample.js"
},
"manifest_version": 3
}

View File

@@ -0,0 +1,95 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// A generic onclick callback function.
chrome.contextMenus.onClicked.addListener(genericOnClick);
// A generic onclick callback function.
function genericOnClick(info, tab) {
switch (info.menuItemId) {
case 'radio':
// Radio item function
console.log('Radio item clicked. Status:', info.checked);
break;
case 'checkbox':
// Checkbox item function
console.log('Checkbox item clicked. Status:', info.checked);
break;
default:
// Standard context menu item function
console.log('Standard context menu item clicked.');
}
}
chrome.runtime.onInstalled.addListener(function () {
// Create one test item for each context type.
let contexts = [
'page',
'selection',
'link',
'editable',
'image',
'video',
'audio'
];
for (let i = 0; i < contexts.length; i++) {
let context = contexts[i];
let title = "Test '" + context + "' menu item";
let id = chrome.contextMenus.create({
title: title,
contexts: [context],
id: context
});
}
// Create a parent item and two children.
let parent = chrome.contextMenus.create({
title: 'Test parent item',
id: 'parent'
});
let child1 = chrome.contextMenus.create({
title: 'Child 1',
parentId: parent,
id: 'child1'
});
let child2 = chrome.contextMenus.create({
title: 'Child 2',
parentId: parent,
id: 'child2'
});
// Create a radio item.
let radio1 = chrome.contextMenus.create({
title: 'radio',
type: 'radio',
id: 'radio'
});
// Create a checkbox item.
let checkbox1 = chrome.contextMenus.create({
title: 'checkbox',
type: 'checkbox',
id: 'checkbox'
});
// Intentionally create an invalid item, to show off error checking in the
// create callback.
chrome.contextMenus.create(
{ title: 'Oops', parentId: 999, id: 'errorItem' },
function () {
if (chrome.runtime.lastError) {
console.log('Got expected error: ' + chrome.runtime.lastError.message);
}
}
);
});