Skip to content

Commit f08fc13

Browse files
committed
WIP translate - cross-window-communication
1 parent ce10f29 commit f08fc13

1 file changed

Lines changed: 56 additions & 56 deletions

File tree

  • 4-frames-and-windows/03-cross-window-communication

4-frames-and-windows/03-cross-window-communication/article.md

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,123 @@
1-
# Cross-window communication
1+
# ウィンドウ間のやり取り
22

3-
The "Same Origin" (same site) policy limits access of windows and frame to each other.
3+
"同一オリジン" (同一サイト) ポリシーは、ウィンドウとフレームのアクセスを互いに制限します。
44

5-
The idea is that if we have two windows open: one from `john-smith.com`, and another one is `gmail.com`, then we wouldn't want a script from `john-smith.com` to read our mail.
5+
2つのウィンドウが開いているとします: 1つは `john-smith.com`、もう1つは `gmail.com` です。この場合、`john-smith.com` がメールを読むようなスクリプトは望まないでしょう。
66

77
[cut]
88

9-
## Same Origin
9+
## 同一オリジン(Same Origin)
1010

11-
Two URLs are said to have the "same origin" if they have the same protocol, domain and port.
11+
同じプロトコル、ドメインとポートを持つ場合、2つの URL は "同一オリジン" 言われます。
1212

13-
These URLs all share the same origin:
13+
これらの URL はすべて同じオリジンです:
1414

1515
- `http://site.com`
1616
- `http://site.com/`
1717
- `http://site.com/my/page.html`
1818

19-
These ones do not:
19+
これらは違います:
2020

21-
- <code>http://<b>www.</b>site.com</code> (another domain: `www.` matters)
22-
- <code>http://<b>site.org</b></code> (another domain: `.org` matters)
23-
- <code><b>https://</b>site.com</code> (another protocol: `https`)
24-
- <code>http://site.com:<b>8080</b></code> (another port: `8080`)
21+
- <code>http://<b>www.</b>site.com</code> (別のドメイン: `www.` のため)
22+
- <code>http://<b>site.org</b></code> (別のドメイン: `.org` のため)
23+
- <code><b>https://</b>site.com</code> (別のプロトコル: `https`)
24+
- <code>http://site.com:<b>8080</b></code> (別のポート: `8080`)
2525

26-
If we have a reference to another window (a popup or iframe), and that window comes from the same origin, then we can do everything with it.
26+
"同一オリジン" ポリシーは次のようになります:
2727

28-
If it comes from another origin, then we can only change its location. Please note: not *read* the location, but *modify* it, redirect it to another place. That's safe, because the URL may contain sensitive parameters, so reading it from another origin is prohibited, but changing is not.
28+
- 別のウィンドウへの参照があり(e.g. `window.open` によって作られたポップアップ、あるいは `<iframe>` 内のウィンドウ)、そのウィンドウが同一オリジンから来ている場合は、そのウィンドウへのへのフルアクセスを持ちます。
29+
- そうではなく、別のオリジンから来たものである場合、そのウィンドウの内容にアクセスすることはできません。: 変数、ドキュメント、その他すべて。唯一の例外は `location` です: それは変えることができます(結果、ユーザをリダイレクトします)。しかし、location を *読む* ことはできません(したがって、ユーザが今どこにいるのかを知ることはできず、情報が漏れることはありません)。
2930

30-
Also such windows may exchange messages. Soon about that later.
31+
それでは、いくつか例を見てみましょう。まず、同じオリジンから来て、"同一オリジン" ポリシーに衝突しないページを見ます。その後、"同一オリジン" ポリシーを回避することができる、ウィンドウ間のメッセージングについて説明します。
3132

32-
````warn header="Exclusion: subdomains may be same-origin"
33+
````warn header="サブドメインは同一オリジンの場合があります"
34+
"同一オリジン" ポリシーには、小さな例外があります。
3335
34-
There's an important exclusion in the same-origin policy.
36+
ウィンドウが同じ第2レベルのドメインを共有している場合、例えば `john.site.com`, `peter.site.com` と `site.com` (これらの共通の第2レベルのドメインは `site.com` です)、これらは "同一オリジン" から来ているものとして扱う事ができます。
3537
36-
If windows share the same second-level domain, for instance `john.site.com`, `peter.site.com` and `site.com`, we can use JavaScript to assign to `document.domain` their common second-level domain `site.com`. Then these windows are treated as having the same origin.
37-
38-
In other words, all such documents (including the one from `site.com`) should have the code:
38+
それを機能させるためには、このようなすべてのページ(`site.com` からのものも含む)は、次のコードを実行する必要があります:
3939
4040
```js
4141
document.domain = 'site.com';
4242
```
4343
44-
Then they can interact without limitations.
45-
46-
That's only possible for pages with the same second-level domain.
44+
これだけです。これで制限なしにやり取りすることができます。繰り返しますが、これは同じ第2レベルのどマインをもつページでのみ可能です。
4745
````
4846

49-
## Accessing an iframe contents
47+
## iframe のコンテンツにアクセスする
5048

51-
An `<iframe>` is a two-faced beast. From one side it's a tag, just like `<script>` or `<img>`. From the other side it's a window-in-window.
49+
最初の例では iframe を説明します。`<iframe>` は二面のある獣です。それは `<script>` あるいは `<img>` と同じような単なるタグである一方、ウィンドウ内のウィンドウです。
5250

53-
The embedded window has a separate `document` and `window` objects.
51+
埋め込みのウィンドウは別の `document` `window` オブジェクトを持ちます。
5452

55-
We can access them like using the properties:
53+
プロパティを使って、それらにアクセスできます。:
5654

57-
- `iframe.contentWindow` is a reference to the window inside the `<iframe>`.
58-
- `iframe.contentDocument` is a reference to the document inside the `<iframe>`.
55+
- `iframe.contentWindow` `<iframe>` 内のウィンドウへの参照です。
56+
- `iframe.contentDocument` `<iframe>` 内のドキュメントへの参照です。
5957

60-
When we access an embedded window, the browser checks if the iframe has the same origin. If that's not so then the access is denied (with exclusions noted above).
58+
埋め込みのウィンドウへアクセスする際、ブラウザは iframe が同一オリジンかをチェックします。もし同一でない場合、アクセスは拒否されます(上で述べた例外を除く)。
6159

62-
For instance, here's an `<iframe>` from another origin:
60+
例えば、これは別のオリジンの `<iframe>` です。:
6361

6462
```html run
6563
<iframe src="https://example.com" id="iframe"></iframe>
6664

6765
<script>
6866
iframe.onload = function() {
69-
// we can get the reference to the inner window
67+
// 内部のウィンドウへの参照を取得できます
7068
let iframeWindow = iframe.contentWindow;
7169
7270
try {
73-
// ...but not to the document inside it
71+
// ...が、その中のドキュメントは取得できません
7472
let doc = iframe.contentDocument;
7573
} catch(e) {
76-
alert(e); // Security Error (another origin)
74+
alert(e); // セキュリティエラー(別オリジン)
7775
}
7876
79-
// also we can't read the URL of the page in it
77+
// その中のページの URL を見ることもできません
8078
try {
8179
alert(iframe.contentWindow.location);
8280
} catch(e) {
83-
alert(e); // Security Error
81+
alert(e); // セキュリティエラー
8482
}
8583
86-
// ...but we can change it (and thus load something else into the iframe)!
87-
iframe.contentWindow.location = '/'; // works
84+
// ...しかし、変更(し、iframe 内になにかをロード)することはできます!
85+
iframe.contentWindow.location = '/'; // 動作します
8886
89-
iframe.onload = null; // clear the handler, to run this code only once
87+
iframe.onload = null; // 一度だけ実行させるためにハンドラをクリア
9088
};
9189
</script>
9290
```
9391

94-
The code above shows errors for any operations except:
92+
上のコードは、以下を除く操作に対してエラーを表示します。:
9593

96-
- Getting the reference to the inner window `iframe.contentWindow`
97-
- Changing its `location`.
94+
- 内部のウィンドウ `iframe.contentWindow` への参照を取得する
95+
- その `location` を変更する
9896

9997
```smart header="`iframe.onload` vs `iframe.contentWindow.onload`"
100-
The `iframe.onload` event is actually the same as `iframe.contentWindow.onload`. It triggers when the embedded window fully loads with all resources.
98+
`iframe.onload` イベントは実際には `iframe.contentWindow.onload` と同じです。これは埋め込みウィンドウがすべてのリソース含め完全に読み込まれた時にトリガーされます。
10199

102-
...But `iframe.onload` is always available, while `iframe.contentWindow.onload` needs the same origin.
100+
...しかし、`iframe.onload` は常に利用可能な一方、`iframe.contentWindow.onload` は同一オリジンである必要があります。
103101
```
104102
105-
And now an example with the same origin. We can do anything with the embedded window:
103+
そして、これは同一オリジンの例です。埋め込みウィンドウでなんでもできます。:
106104
107105
```html run
108106
<iframe src="/" id="iframe"></iframe>
109107
110108
<script>
111109
iframe.onload = function() {
112-
// just do anything
110+
// なんでもできます
113111
iframe.contentDocument.body.prepend("Hello, world!");
114112
};
115113
</script>
116114
```
117115

118-
### Please wait until the iframe loads
119-
120-
When an iframe is created, it immediately has a document. But that document is different from the one that finally loads into it!
116+
### iframe がロードされるまで待ってください
121117

122-
Here, look:
118+
iframe が作成されると、すぐにドキュメントを持ちます。しかし、そのドキュメントは最終的にそこにロードされるものとは異なります!
123119

120+
ここで見てください:
124121

125122
```html run
126123
<iframe src="/" id="iframe"></iframe>
@@ -130,18 +127,18 @@ Here, look:
130127
iframe.onload = function() {
131128
let newDoc = iframe.contentDocument;
132129
*!*
133-
// the loaded document is not the same as initial!
130+
// ロードされたドキュメントは初期のものとは同じではありません!
134131
alert(oldDoc == newDoc); // false
135132
*/!*
136133
};
137134
</script>
138135
```
139136

140-
That's actually a well-known pitfall for novice developers. We shouldn't work with the document immediately, because that's the *wrong document*. If we set any event handlers on it, they will be ignored.
137+
これは実際に、開発者の間でよく知られた落とし穴です。それは *間違ったドキュメント* なので、すぐにドキュメントを使った処理をするべきではありません。もしそこに任意のイベントハンドラを設定しても無視されます。
141138

142-
...But the `onload` event triggers when the whole iframe with all resources is loaded. What if we want to act sooner, on `DOMContentLoaded` of the embedded document?
139+
...しかし、`onload` イベントはすべてのリソースを含む iframe 全体がロードされたときにトリガーされます。仮により早く、埋め込みドキュメントの `DOMContentLoaded` でなにかしたい場合どうすればよいでしょうか?
143140

144-
That's not possible if the iframe comes from another origin. But for the same origin we can try to catch the moment when a new document appears, and then setup necessary handlers, like this:
141+
iframe が別のオリジンから来ている場合は不可能です。しかし、同一オリジンの場合は、次のように新しいドキュメントが現れる瞬間を捉えて、必要なハンドラの設定を試みることができます。:
145142

146143
```html run
147144
<iframe src="/" id="iframe"></iframe>
@@ -209,7 +206,7 @@ if (window == top) { // current window == window.top?
209206

210207
## The sandbox attribute
211208

212-
The `sandbox` attribute allows to forbid certain actions inside an `<iframe>`, to run an untrusted code. It "sandboxes" the iframe by treating it as coming from another origin and/or applying other limitations.
209+
The `sandbox` attribute allows for the exclusion of certain actions inside an `<iframe>` in order to prevent it executing untrusted code. It "sandboxes" the iframe by treating it as coming from another origin and/or applying other limitations.
213210

214211
By default, for `<iframe sandbox src="...">` the "default set" of restrictions is applied to the iframe. But we can provide a space-separated list of "excluded" limitations as a value of the attribute, like this: `<iframe sandbox="allow-forms allow-popups">`. The listed limitations are not applied.
215212

@@ -249,7 +246,9 @@ The purpose of the `"sandbox"` attribute is only to *add more* restrictions. It
249246

250247
The `postMessage` interface allows windows to talk to each other no matter which origin they are from.
251248

252-
It has two parts.
249+
So, it's a way around the "Same Origin" policy. It allows a window from `john-smith.com` to talk to `gmail.com` and exchange information, but only if they both agree and call corresponding Javascript functions. That makes it safe for users.
250+
251+
The interface has two parts.
253252

254253
### postMessage
255254

@@ -351,6 +350,7 @@ Otherwise, only possible actions are:
351350
- Change the location of another window (write-only access).
352351
- Post a message to it.
353352

353+
354354
Exclusions are:
355355
- Windows that share the same second-level domain: `a.site.com` and `b.site.com`. Then setting `document.domain='site.com'` in both of them puts them into the "same origin" state.
356356
- If an iframe has a `sandbox` attribute, it is forcefully put into the "different origin" state, unless the `allow-same-origin` is specified in the attribute value. That can be used to run untrusted code in iframes from the same site.
@@ -364,4 +364,4 @@ The `postMessage` interface allows two windows to talk with security checks:
364364
- `source` -- the reference to the sender window.
365365
- `data` -- the data, any object in everywhere except IE that supports only strings.
366366

367-
We should use `addEventListener` to set the handler for this event inside the target window.
367+
We should use `addEventListener` to set the handler for this event inside the target window.

0 commit comments

Comments
 (0)