aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/mozilla/add-ons/webextensions/api/tabs/insertcss
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/mozilla/add-ons/webextensions/api/tabs/insertcss
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/mozilla/add-ons/webextensions/api/tabs/insertcss')
-rw-r--r--files/zh-cn/mozilla/add-ons/webextensions/api/tabs/insertcss/index.html129
1 files changed, 129 insertions, 0 deletions
diff --git a/files/zh-cn/mozilla/add-ons/webextensions/api/tabs/insertcss/index.html b/files/zh-cn/mozilla/add-ons/webextensions/api/tabs/insertcss/index.html
new file mode 100644
index 0000000000..5ea7d5205c
--- /dev/null
+++ b/files/zh-cn/mozilla/add-ons/webextensions/api/tabs/insertcss/index.html
@@ -0,0 +1,129 @@
+---
+title: tabs.insertCSS()
+slug: Mozilla/Add-ons/WebExtensions/API/tabs/insertCSS
+tags:
+ - 注入CSS
+translation_of: Mozilla/Add-ons/WebExtensions/API/tabs/insertCSS
+---
+<div>{{AddonSidebar()}}</div>
+
+<p>向一个页面注入CSS</p>
+
+<p>使用该API前你必须拥有目标页面的权限, 可以是 <a href="https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/permissions#Host_permissions">主机权限</a>, 或者使用 <a href="/en-US/Add-ons/WebExtensions/manifest.json/permissions#activeTab_permission">activeTab 权限</a>.</p>
+
+<p>你只能向符合 <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns">match pattern </a>的网页注入CSS: 其形式必定是 "http", "https", "file", "ftp" 之一. 你不能向任何浏览器内置页面注入CSS, 比如 about:debugging, about:addons, 或者你打开的一个新的空白页。</p>
+
+<p>当再次调用{{WebExtAPIRef("tabs.removeCSS()")}} 时,已经注入的CSS可能会被清除。</p>
+
+<p>这是一个返回<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code> 的异步函数。</p>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox brush:js">var inserting = browser.tabs.insertCSS(
+ tabId, // optional integer
+ details // extensionTypes.InjectDetails
+)
+</pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt><code>tabId</code> {{optional_inline}}</dt>
+ <dd><code>integer,</code> 将要注入css的标签ID。默认为当前窗口的活动标签。</dd>
+ <dt><code>details</code></dt>
+ <dd>{{WebExtAPIRef('extensionTypes.InjectDetails')}}. 对注入的描述,包含以下属性:</dd>
+ <dd>
+ <dl class="reference-values">
+ <dt><code>allFrames</code>{{optional_inline}}</dt>
+ <dd><code>boolean</code>. 如果为真,该CSS会被注入到该页面的所有框架,如果为假,Css只会注入到最顶层框架,默认为假。</dd>
+ <dt><code>code</code>{{optional_inline}}</dt>
+ <dd><code>string</code>. 将要注入的代码。</dd>
+ <dt><code>file</code>{{optional_inline}}</dt>
+ <dd><code>string</code>. 包含将要注入代码的文件路径,在Firefox中,相对URLs 决定于当前页面的URL,在Chrome中,决定于扩展的基础URL。为了跨浏览器工作,你应该使用一个从扩展根目录开始的绝对路径,比如 : <code>"/path/to/stylesheet.css"</code>.</dd>
+ <dt><code>frameId</code>{{optional_inline}}</dt>
+ <dd><code>integer</code>. CSS应该被注入的框架. 默认为 <code>0</code> (顶层框架).</dd>
+ <dt><code>matchAboutBlank</code>{{optional_inline}}</dt>
+ <dd><code>boolean</code>. If <code>true</code>, the code will be injected into embedded "about:blank" and "about:srcdoc" frames if your add-on has access to their parent document. The code cannot be inserted in top-level about: frames. Defaults to <code>false</code>.</dd>
+ <dt><code>runAt</code>{{optional_inline}}</dt>
+ <dd>{{WebExtAPIRef('extensionTypes.RunAt')}}. The soonest that the code will be injected into the tab. Defaults to "document_idle".</dd>
+ </dl>
+ </dd>
+</dl>
+
+<h3 id="Return_value">Return value</h3>
+
+<p> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code> 将会在CSS成功注入时 被填充,如果有任何错误发生,promise将会被注入一个错误消息。</p>
+
+<h2 id="Browser_compatibility">Browser compatibility</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.tabs.insertCSS")}}</p>
+
+<h2 id="例子">例子</h2>
+
+<p>下面例子将通过字符串变量形式向当前活动标签注入一段CSS代码</p>
+
+<pre class="brush: js">var css = "body { border: 20px dotted pink; }";
+
+browser.browserAction.onClicked.addListener(() =&gt; {
+
+ function onError(error) {
+ console.log(`Error: ${error}`);
+ }
+
+ var insertingCSS = browser.tabs.insertCSS({code: css});
+ insertingCSS.then(null, onError);
+});</pre>
+
+<p>下面例子将以通过加载文件形式向页面注入CSS。CSS被注入在ID为2的tab中。</p>
+
+<pre class="brush: js">browser.browserAction.onClicked.addListener(() =&gt; {
+
+ function onError(error) {
+ console.log(`Error: ${error}`);
+ }
+
+ var insertingCSS = browser.tabs.insertCSS(2, {file: "content-style.css"});
+ insertingCSS.then(null, onError);
+});</pre>
+
+<p>{{WebExtExamples}}</p>
+
+<div class="note"><strong>致谢</strong>
+
+<p>本页 API 以谷歌 Chromium的 <a href="https://developer.chrome.com/extensions/tabs#method-insertCSS"><code>chrome.tabs</code></a> API为基础. 该篇文档由Chromium 代码 <a href="https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/api/tabs.json"><code>tabs.json</code></a>衍变而来.</p>
+
+<p>Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.</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>