Skip to content

Commit

Permalink
ja: Format /mozilla/add-ons using Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
queengooborg committed Jul 27, 2023
1 parent 1b3474f commit cbf9c41
Show file tree
Hide file tree
Showing 31 changed files with 415 additions and 455 deletions.
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ build/
/files/ja/learn/css/**/*.md
/files/ja/learn/javascript/**/*.md
/files/ja/learn/server-side/**/*.md
/files/ja/mozilla/**/*.md
/files/ja/mdn/**/*.md
/files/ja/mozilla/add-ons/**/*.md
/files/ja/mozilla/add-ons/webextensions/api/**/*.md
/files/ja/web/api/**/*.md
/files/ja/web/css/**/*.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ WebExtension API では、こうしたボタンの種類は "ブラウザーア

```json
{

"description": "Demonstrating toolbar buttons",
"manifest_version": 2,
"name": "button-demo",
Expand All @@ -39,7 +38,6 @@ WebExtension API では、こうしたボタンの種類は "ブラウザーア
"32": "icons/page-32.png"
}
}

}
```

Expand All @@ -59,7 +57,7 @@ These icons are from the [bitsies!](https://www.iconfinder.com/iconsets/bitsies)
```js
function openPage() {
browser.tabs.create({
url: "https://developer.mozilla.org"
url: "https://developer.mozilla.org",
});
}

Expand All @@ -70,7 +68,7 @@ browser.browserAction.onClicked.addListener(openPage);

ここで完全な拡張機能は次のようです:

```html
```plain
button/
icons/
page-16.png
Expand All @@ -89,7 +87,6 @@ button/

```json
{

"description": "Demonstrating toolbar buttons",
"manifest_version": 2,
"name": "button-demo",
Expand All @@ -103,7 +100,6 @@ button/
"32": "icons/page-32.png"
}
}

}
```

Expand All @@ -116,21 +112,20 @@ button/
さてポップアップを作らねばなりません。"popup" というディレクトリーを作成してその中に "choose_page.html" というファイルを作ります。中身は次の通り:

```html
<!DOCTYPE html>
<!doctype html>

<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="choose_page.css"/>
<meta charset="utf-8" />
<link rel="stylesheet" href="choose_page.css" />
</head>

<body>
<div class="page-choice">developer.mozilla.org</div>
<div class="page-choice">support.mozilla.org</div>
<div class="page-choice">addons.mozilla.org</div>
<script src="choose_page.js"></script>
</body>

<body>
<div class="page-choice">developer.mozilla.org</div>
<div class="page-choice">support.mozilla.org</div>
<div class="page-choice">addons.mozilla.org</div>
<script src="choose_page.js"></script>
</body>
</html>
```

Expand All @@ -139,7 +134,8 @@ button/
"popup" ディレクトリーに"choose_page.css" というファイルを作って、次の中身を入れます:

```css
html, body {
html,
body {
width: 300px;
}

Expand All @@ -152,7 +148,7 @@ html, body {
}

.page-choice:hover {
background-color: #CFF2F2;
background-color: #cff2f2;
}
```

Expand All @@ -161,16 +157,15 @@ html, body {
次に、"popup" ディレクトリーに"choose_page.js" ファイルを作成し、次の中身を入れます:

```js
document.addEventListener("click", function(e) {
document.addEventListener("click", function (e) {
if (!e.target.classList.contains("page-choice")) {
return;
}

var chosenPage = "https://" + e.target.textContent;
browser.tabs.create({
url: chosenPage
url: chosenPage,
});

});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ Webextension を用いた拡張機能は Chrome や Opera の拡張機能と互
Chrome では、拡張機能は `chrome` ネームスペースを使って特権 JavaScript API にアクセスします:

```js
chrome.browserAction.setIcon({path: "path/to/icon.png"});
chrome.browserAction.setIcon({ path: "path/to/icon.png" });
```

WebExtensions は同等の API に `browser` ネームスペースを使ってアクセスします:

```js
browser.browserAction.setIcon({path: "path/to/icon.png"});
browser.browserAction.setIcon({ path: "path/to/icon.png" });
```

多くの API は非同期です。 Chrome では、非同期 API はコールバックを使用して値を返し、{{WebExtAPIRef("runtime.lastError")}}がエラーを通知します:
Expand All @@ -36,10 +36,7 @@ function logCookie(c) {
}
}

chrome.cookies.set(
{url: "https://developer.mozilla.org/"},
logCookie
);
chrome.cookies.set({ url: "https://developer.mozilla.org/" }, logCookie);
```

同様の WebExtensions API では [promises](/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise) を利用します:
Expand All @@ -53,9 +50,7 @@ function logError(e) {
console.error(e);
}

var setCookie = browser.cookies.set(
{url: "https://developer.mozilla.org/"}
);
var setCookie = browser.cookies.set({ url: "https://developer.mozilla.org/" });
setCookie.then(logCookie, logError);
```

Expand Down
80 changes: 44 additions & 36 deletions files/ja/mozilla/add-ons/webextensions/content_scripts/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ l10n:

次の 3 つの方法のいずれかを使用して、ウェブページにコンテンツスクリプトを読み込むことができます。

1. - インストール時に、URL パターンに一致するページ内へ。
- : `manifest.json`[`content_scripts`](/ja/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) キーを使用して、URL が[指定されたパターンに一致する](/ja/docs/Mozilla/Add-ons/WebExtensions/Match_patterns)ページをロードするたびにコンテンツスクリプトを読み込むようブラウザーに依頼できます。
2. - 実行時に、URL パターンに一致するページ内へ。
- : {{WebExtAPIRef("contentScripts")}} API を使って、URL が[指定されたパターンに一致する](/ja/docs/Mozilla/Add-ons/WebExtensions/Match_patterns)ページを読み込むたびにコンテンツスクリプトを読み込むようブラウザーに依頼できます。これは方法 1 と似ていますが、実行時にコンテンツスクリプトを追加/削除できる点が異なります。)
3. - 実行時に、特定のタブへ。
- : Manifest V2 において、[`tabs.executeScript()`](/ja/docs/Mozilla/Add-ons/WebExtensions/API/tabs/executeScript) API を使用するか、Manifest V3 において、{{WebExtAPIRef("scripting.executeScript()")}} を使用すると、必要なときにコンテンツスクリプトを特定のタブに読み込むことができます。(ユーザーが[ブラウザーアクション](/ja/docs/Mozilla/Add-ons/WebExtensions/user_interface/Browser_action)をクリックした場合など。)
1. インストール時に、URL パターンに一致するページ内へ。
- : `manifest.json`[`content_scripts`](/ja/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts) キーを使用して、URL が[指定されたパターンに一致する](/ja/docs/Mozilla/Add-ons/WebExtensions/Match_patterns)ページをロードするたびにコンテンツスクリプトを読み込むようブラウザーに依頼できます。
2. 実行時に、URL パターンに一致するページ内へ。
- : {{WebExtAPIRef("contentScripts")}} API を使って、URL が[指定されたパターンに一致する](/ja/docs/Mozilla/Add-ons/WebExtensions/Match_patterns)ページを読み込むたびにコンテンツスクリプトを読み込むようブラウザーに依頼できます。これは方法 1 と似ていますが、実行時にコンテンツスクリプトを追加/削除できる点が異なります。)
3. 実行時に、特定のタブへ。
- : Manifest V2 において、[`tabs.executeScript()`](/ja/docs/Mozilla/Add-ons/WebExtensions/API/tabs/executeScript) API を使用するか、Manifest V3 において、{{WebExtAPIRef("scripting.executeScript()")}} を使用すると、必要なときにコンテンツスクリプトを特定のタブに読み込むことができます。(ユーザーが[ブラウザーアクション](/ja/docs/Mozilla/Add-ons/WebExtensions/user_interface/Browser_action)をクリックした場合など。)

*フレームごと、拡張機能ごとの*グローバルスコープしかありません。これは 1 つのコンテンツスクリプトの変数は、読み込み方にかかわらず、他のコンテンツスクリプトからアクセスできることになります。

Expand All @@ -72,7 +72,7 @@ Firefox では、この挙動は [Xray vision](/ja/docs/Mozilla/Add-ons/WebExten
次のようなウェブページを考えてみてください。

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Expand Down Expand Up @@ -101,7 +101,7 @@ window.foo = "This global variable was added by a page script";
// redefine the built-in window.confirm() function
window.confirm = () => {
alert("The page script has also redefined 'confirm'");
}
};
```

今度は拡張機能がページにコンテンツスクリプトを挿入します。
Expand All @@ -114,7 +114,7 @@ let pageScriptPara = document.getElementById("page-script-para");
pageScriptPara.style.backgroundColor = "blue";

// can't see properties added by page-script.js
console.log(window.foo); // undefined
console.log(window.foo); // undefined

// sees the original form of redefined properties
window.confirm("Are you sure?"); // calls the original window.confirm()
Expand Down Expand Up @@ -272,7 +272,7 @@ function notifyExtension(e) {
if (e.target.tagName !== "A") {
return;
}
browser.runtime.sendMessage({"url": e.target.href});
browser.runtime.sendMessage({ url: e.target.href });
}
```

Expand All @@ -285,10 +285,10 @@ browser.runtime.onMessage.addListener(notify);

function notify(message) {
browser.notifications.create({
"type": "basic",
"iconUrl": browser.extension.getURL("link.png"),
"title": "You clicked a link!",
"message": message.url
type: "basic",
iconUrl: browser.extension.getURL("link.png"),
title: "You clicked a link!",
message: message.url,
});
}
```
Expand Down Expand Up @@ -328,16 +328,16 @@ function notify(message) {
```js
// content-script.js

let myPort = browser.runtime.connect({name:"port-from-cs"});
myPort.postMessage({greeting: "hello from content script"});
let myPort = browser.runtime.connect({ name: "port-from-cs" });
myPort.postMessage({ greeting: "hello from content script" });

myPort.onMessage.addListener((m) => {
console.log("In content script, received message from background script: ");
console.log(m.greeting);
});

document.body.addEventListener("click", () => {
myPort.postMessage({greeting: "they clicked the page!"});
myPort.postMessage({ greeting: "they clicked the page!" });
});
```

Expand All @@ -359,16 +359,18 @@ let portFromCS;

function connected(p) {
portFromCS = p;
portFromCS.postMessage({greeting: "hi there content script!"});
portFromCS.postMessage({ greeting: "hi there content script!" });
portFromCS.onMessage.addListener((m) => {
portFromCS.postMessage({greeting: `In background script, received message from content script: ${m.greeting}`});
portFromCS.postMessage({
greeting: `In background script, received message from content script: ${m.greeting}`,
});
});
}

browser.runtime.onConnect.addListener(connected);

browser.browserAction.onClicked.addListener(() => {
portFromCS.postMessage({greeting: "they clicked the button!"});
portFromCS.postMessage({ greeting: "they clicked the button!" });
});
```

Expand All @@ -379,19 +381,19 @@ browser.browserAction.onClicked.addListener(() => {
```js
// background-script.js

let ports = []
let ports = [];

function connected(p) {
ports[p.sender.tab.id] = p
ports[p.sender.tab.id] = p;
//
}

browser.runtime.onConnect.addListener(connected)
browser.runtime.onConnect.addListener(connected);

browser.browserAction.onClicked.addListener(() => {
ports.forEach((p) => {
p.postMessage({greeting: "they clicked the button!"})
})
p.postMessage({ greeting: "they clicked the button!" });
});
});
```

Expand Down Expand Up @@ -422,10 +424,13 @@ let messenger = document.getElementById("from-page-script");
messenger.addEventListener("click", messageContentScript);

function messageContentScript() {
window.postMessage({
direction: "from-page-script",
message: "Message from the page"
}, "*");
window.postMessage(
{
direction: "from-page-script",
message: "Message from the page",
},
"*",
);
}
```

Expand Down Expand Up @@ -481,15 +486,18 @@ window.addEventListener("message", (event) => {
```js
// content-script.js

window.eval('window.x = 1;');
eval('window.y = 2');
window.eval("window.x = 1;");
eval("window.y = 2");

console.log(`In content script, window.x: ${window.x}`);
console.log(`In content script, window.y: ${window.y}`);

window.postMessage({
message: "check"
}, "*");
window.postMessage(
{
message: "check",
},
"*",
);
```
このコードは単に変数 x と y を、`window.eval()``eval()` を用いて作成し、値をログに出し、ページにメッセージします。
Expand Down Expand Up @@ -536,11 +544,11 @@ In page script, window.y: undefined
>
> console.log = () => {
> original(true);
> }
> };
> ```
>
> ```js example-bad
> // content-script.js calls the redefined version
>
> window.eval('console.log(false)');
> window.eval("console.log(false)");
> ```
Loading

0 comments on commit cbf9c41

Please sign in to comment.