1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
---
title: menus.create()
slug: Mozilla/Add-ons/WebExtensions/API/contextMenus/create
translation_of: Mozilla/Add-ons/WebExtensions/API/menus/create
---
<div>{{AddonSidebar()}}</div>
<p>주어진 객체대로 새 메뉴 항목을 만든다.</p>
<p>이 함수는 다른 비공기 함수들과 달리 promise가 아니라 새 항목의 ID를 반환한다. 성공과 실패에 대한 처리는 필요하면 콜백으로 한다.</p>
<p>다른 브라우저와의 호환성을 위해, <code>menus</code> 이름공간 뿐 아니라 <code>contextMenus</code> 이름공간으로도 이 메소드를 사용할 수 있다. 하지만 <code>contextMenus</code>로는 툴 메뉴 항목(<code>contexts: ["tools_menu"]</code>)은 만들 수 없다.</p>
<h2 id="문법">문법</h2>
<pre class="syntaxbox brush:js">browser.menus.create(
createProperties, // object
function() {...} // optional function
)
</pre>
<h3 id="매개변수">매개변수</h3>
<dl>
<dt><code>createProperties</code></dt>
<dd><code>object</code>. 새 메뉴 항목의 속성들</dd>
<dd>
<dl class="reference-values">
<dt><code>checked</code> {{optional_inline}}</dt>
<dd><code>boolean</code>. checkbox나 radio 항목의 초기값: 선택은 <code>true</code>, 선택이 아니면 <code>false</code>. radio 항목이라면 그룹 중에서 하나만 선택된 것으로 할 수 있다.</dd>
<dt><code>command</code> {{optional_inline}}</dt>
<dd>
<p><code>string</code>. 클릭 했을 때 수행할 동작을 기술한다. 가능한 값은:</p>
<ul>
<li><code>"_execute_browser_action"</code>: 확장의 브라우저 액션을 클릭한 것처럼 한다. 팝업이 있으면 팝업이 열린다.</li>
<li><code>"_execute_page_action"</code>: 확장의 페이지 액션을 클릭한 것처럼 한다. 팝업이 있으면 팝업이 열린다.</li>
<li><code>"_execute_sidebar_action"</code>: 확장의 사이드바를 연다.</li>
</ul>
<p>항목을 클릭하면 여전히 {{WebExtAPIRef("menus.onClicked")}} 이벤트도 발생한다. 어느게 먼저 인지는 보장되지 않지만 <code>onClicked</code>이 발생하기 전에 명령이 실행될 것이다.</p>
</dd>
<dt><code>contexts</code> {{optional_inline}}</dt>
<dd>
<p><code>{{WebExtAPIRef('menus.ContextType')}}</code>의 <code>배열</code>. 메뉴 항목이 표시할 콘텍스트의 배열. 생략되면:</p>
<ul>
<li>상위 항목에 콘텍스트가 설정되었으면 그걸 물려받는다.</li>
<li>아니면, 항목은 ["page"]로 설정된다.</li>
</ul>
</dd>
<dt><code>documentUrlPatterns</code> {{optional_inline}}</dt>
<dd><code><code>string</code></code>의 <code>배열</code>. 메뉴 항목의 표시를 URL이 주어진 <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns">match patterns</a>과 일치하는 문서로 제한한다. 프레임에도 적용된다.</dd>
<dt><code>enabled</code> {{optional_inline}}</dt>
<dd><code>boolean</code>. 메뉴 항목이 사용 가능한지 아닌지를 지정한다. 기본값은 <code>true</code>.</dd>
<dt><code>icons</code> {{optional_inline}}</dt>
<dd>
<p><code>object</code>. One or more custom icons to display next to the item. Custom icons can only be set for items appearing in submenus. This property is an object with one property for each supplied icon: the property's name should include the icon's size in pixels, and path is relative to the icon from the extension's root directory. The browser tries to choose a 16x16 pixel icon for a normal display or a 32x32 pixel icon for a high-density display. To avoid any scaling, you can specify icons like this:</p>
<pre class="brush: json no-line-numbers language-json"><code class="language-json"><span class="key token">"icons":</span> <span class="punctuation token">{</span>
<span class="key token">"16":</span> <span class="string token">"path/to/geo-16.png"</span><span class="punctuation token">,</span>
<span class="key token">"32":</span> <span class="string token">"path/to/geo-32.png"</span>
<span class="punctuation token">}</span></code></pre>
<p>Alternatively, you can specify a single SVG icon, and it will be scaled appropriately:</p>
<pre class="brush: json no-line-numbers language-json"><code class="language-json"><span class="key token">"icons":</span> <span class="punctuation token">{</span>
<span class="key token">"16":</span> <span class="string token">"path/to/geo.svg"</span>
<span class="punctuation token">}</span></code></pre>
<div class="blockIndicator note">
<p><strong>Note</strong>: The top-level menu item uses the <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/icons">icons</a> specified in the manifest rather than what is specified with this key.</p>
</div>
</dd>
<dt><code>id</code> {{optional_inline}}</dt>
<dd><code>string</code>. The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension.</dd>
<dt><code>onclick</code> {{optional_inline}}</dt>
<dd><code>function</code>. A function that will be called when the menu item is clicked. Event pages cannot use this: instead, they should register a listener for {{WebExtAPIRef('menus.onClicked')}}.</dd>
<dt><code>parentId</code> {{optional_inline}}</dt>
<dd><code><code>integer</code></code> or <code><code>string</code></code>. The ID of a parent menu item; this makes the item a child of a previously added item. Note: If you have created more than one menu item, then the items will be placed in a submenu. The submenu's parent will be labeled with the name of the extension.</dd>
<dt><code>targetUrlPatterns</code> {{optional_inline}}</dt>
<dd><code><code>string</code></code>의 <code>배열</code>. <code>documentUrlPatterns</code> 비슷한데, anchor 태그의 <code>href</code> 속성과 img/audio/video 태그의 <code>src</code> 속성에 기초해 걸러지는 것이다. 여기서 URL scheme는 뭐라도 상관없다. 설사 match pattern으로 보통은 허용되지 않는 것이라도 된다.</dd>
<dt><code>title</code> {{optional_inline}}</dt>
<dd>
<p><code>string</code>. The text to be displayed in the item. Mandatory unless <code>type</code> is "separator".</p>
<p>You can use "%s" in the string. If you do this in a menu item, and some text is selected in the page when the menu is shown, then the selected text will be interpolated into the title. For example, if <code>title</code> is "Translate '%s' to Pig Latin" and the user selects the word "cool", then activates the menu, then the menu item's title will be: "Translate 'cool' to Pig Latin".</p>
<p>If the title contains an ampersand "&" then the next character will be used as an access key for the item, and the ampersand will not be displayed. Exceptions to this are:</p>
<ul>
<li>If the next character is also an ampersand: then a single ampersand will be displayed and no access key will be set. In effect, "&&" is used to display a single ampersand.</li>
<li>If the next characters are the interpolation directive "%s": then the ampersand will not be displayed and no access key will be set.</li>
<li>If the ampersand is the last character in the title: then the ampersand will not be displayed and no access key will be set.</li>
</ul>
<p>Only the first ampersand will be used to set an access key: subsequent ampersands will not be displayed but will not set keys. So "&A and &B" will be shown as "A and B" and set "A" as the access key.</p>
</dd>
<dt><code>type</code> {{optional_inline}}</dt>
<dd>{{WebExtAPIRef('menus.ItemType')}}. The type of menu item: "normal", "checkbox", "radio", "separator". Defaults to "normal".</dd>
<dt><code>visible</code> {{optional_inline}}</dt>
<dd><code>boolean</code>. Whether the item is shown in the menu. Defaults to <code>true</code>.</dd>
</dl>
</dd>
<dt><code>callback</code> {{optional_inline}}</dt>
<dd><code>function</code>. Called when the item has been created. If there were any problems creating the item, details will be available in {{WebExtAPIRef('runtime.lastError')}}.</dd>
</dl>
<h3 id="Return_value">Return value</h3>
<p><code><code>integer</code></code> or <code><code>string</code></code>. The ID of the newly created item.</p>
<h2 id="브라우저_호환성">브라우저 호환성</h2>
<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
<p>{{Compat("webextensions.api.menus.create", 10)}}</p>
<h2 id="예제">예제</h2>
<p>이 예제는 페이지에 선택된 텍스트가 있을 때 표시되는 콘텍스트 메뉴 항목을 만든다. 동작은 선택된 텍스트를 콘솔에 로그로 남기는 것이다:</p>
<pre class="brush: js">browser.menus.create({
id: "log-selection",
title: "Log '%s' to the console",
contexts: ["selection"]
});
browser.menus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "log-selection") {
console.log(info.selectionText);
}
});</pre>
<p>이 예제는 두 개의 radio 항목을 추가한다. 선택해서 테두리의 색을 녹색이나 청색으로 할 수 있다. 이 예제는 <a href="/en-US/Add-ons/WebExtensions/manifest.json/permissions#activeTab_permission">activeTab 권한</a>이 필요하다.</p>
<pre class="brush: js">function onCreated() {
if (browser.runtime.lastError) {
console.log("error creating item:" + browser.runtime.lastError);
} else {
console.log("item created successfully");
}
}
browser.menus.create({
id: "radio-green",
type: "radio",
title: "Make it green",
contexts: ["all"],
checked: false
}, onCreated);
browser.menus.create({
id: "radio-blue",
type: "radio",
title: "Make it blue",
contexts: ["all"],
checked: false
}, onCreated);
var makeItBlue = 'document.body.style.border = "5px solid blue"';
var makeItGreen = 'document.body.style.border = "5px solid green"';
browser.menus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == "radio-blue") {
browser.tabs.executeScript(tab.id, {
code: makeItBlue
});
} else if (info.menuItemId == "radio-green") {
browser.tabs.executeScript(tab.id, {
code: makeItGreen
});
}
});</pre>
<p>{{WebExtExamples}}</p>
<div class="note"><strong>Acknowledgements</strong>
<p>This API is based on Chromium's <a href="https://developer.chrome.com/extensions/contextMenus#method-create"><code>chrome.contextMenus</code></a> API. This documentation is derived from <a href="https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/api/context_menus.json"><code>context_menus.json</code></a> in the Chromium code.</p>
</div>
<div class="hidden">
<pre>// Copyright 2015 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>
</div>
|