اختصارات لوحة المفاتيح باستخدام Commands API
وفّر للمستخدمين المحترفين وصولًا سريعًا إلى إضافتك عبر اختصارات لوحة مفاتيح عامة معرّفة باستخدام commands API.
اختصارات لوحة المفاتيح باستخدام Commands API درس مجاني في Browser Extensions Development (Chrome & Edge) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Browser Extensions Development (Chrome & Edge)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Keyboard Shortcuts
Beyond context menus and the omnibox, the commands API lets users trigger actions with a keystroke. Shortcuts make your extension feel fast and professional to power users.
Declaring Commands
Define each command in the manifest under commands, with a suggested key combination per platform.
{
"commands": {
"toggle-feature": {
"suggested_key": { "default": "Ctrl+Shift+Y" },
"description": "Toggle the feature"
}
}
}Cross-Platform Keys
macOS uses Command instead of Ctrl. Provide platform-specific suggestions so the default keys feel native everywhere.
"suggested_key": {
"default": "Ctrl+Shift+Y",
"mac": "Command+Shift+Y"
}Listening for Commands
In your service worker, handle the onCommand event. The command name you declared is passed to the listener.
chrome.commands.onCommand.addListener((command) => {
if (command === 'toggle-feature') {
console.log('Shortcut fired')
}
})The Special _execute_action Command
A reserved command named _execute_action opens your popup or fires the action click. You do not handle it in code; the browser does.
{
"commands": {
"_execute_action": {
"suggested_key": { "default": "Ctrl+Shift+E" }
}
}
}Acting on the Current Tab
Most shortcuts operate on the active tab. Query it inside the handler, then send a message or inject a script.
chrome.commands.onCommand.addListener(async (command) => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true })
chrome.tabs.sendMessage(tab.id, { command })
})Users Can Rebind Keys
Your suggested keys are only suggestions. Users can change them on the browser's extension shortcuts page, and conflicts are resolved there.
// chrome://extensions/shortcuts lets users rebindLimited Number of Suggestions
Browsers limit how many commands can have a suggested key (commonly four). Extra commands still work but start unbound until the user assigns a key.
Reading Registered Commands
Use commands.getAll to inspect what keys are currently bound, useful for showing a help screen in your options page.
const cmds = await chrome.commands.getAll()
cmds.forEach(c => console.log(c.name + ': ' + c.shortcut))Avoiding Conflicts
Pick uncommon combinations to reduce clashes with the browser and other extensions. Combos with Shift plus a letter are usually safe.
Documenting Shortcuts
List your shortcuts in the options page or onboarding so users discover them. A hidden shortcut helps no one.
Quick Check
Test your commands API knowledge.
Recap
You added keyboard shortcuts:
- Declare them under
commandswith per-platform keys - Handle
onCommandin the service worker - Use
_execute_actionfor the popup - Respect the suggested-key limit and user rebinds
- Document shortcuts for discoverability
Shortcuts make your extension feel fast and power-user friendly.
الأسئلة الشائعة
هل درس «اختصارات لوحة المفاتيح باستخدام Commands API» مجاني؟
نعم — نص درس «اختصارات لوحة المفاتيح باستخدام Commands API» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Browser Extensions Development (Chrome & Edge)، انتقل إلى CoddyKit PRO. تتضمن دورة Browser Extensions Development (Chrome & Edge) 4 دروس في المجموع.
ماذا ستتعلم في «اختصارات لوحة المفاتيح باستخدام Commands API»؟
وفّر للمستخدمين المحترفين وصولًا سريعًا إلى إضافتك عبر اختصارات لوحة مفاتيح عامة معرّفة باستخدام commands API. تتمرن على Browser Extensions Development (Chrome & Edge) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Browser Extensions Development (Chrome & Edge)؟
لا تُشترط خبرة سابقة. Browser Extensions Development (Chrome & Edge) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «اختصارات لوحة المفاتيح باستخدام Commands API»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Browser Extensions Development (Chrome & Edge) هذا؟
نعم. كل درس في Browser Extensions Development (Chrome & Edge) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إضافة عناصر إلى قائمة السياق
- دمج الكلمات المفتاحية مع Omnibox
- الاستجابة لإدخالات Omnibox
- اختصارات لوحة المفاتيح باستخدام Commands API