aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/mozilla/add-ons/webextensions/api/tabs/insertcss/index.html
blob: 8466557459606eff8e904dbd0a1da2283b7fbf4e (plain)
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
---
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>{{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>