mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
* 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>
96 lines
2.6 KiB
JavaScript
96 lines
2.6 KiB
JavaScript
// 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);
|
|
}
|
|
}
|
|
);
|
|
});
|