回答編集履歴

1

じっさいのコード

2017/10/14 06:20

投稿

m0a
m0a

スコア708

test CHANGED
@@ -1,3 +1,37 @@
1
1
  locationで見分ける方法があることは思いつきました。
2
2
 
3
+
4
+
5
+ typescriptの例です。
6
+
7
+
8
+
9
+ ```
10
+
11
+ type RunContext = 'content' | 'background' | 'sidebar';
12
+
13
+
14
+
15
+ // 実行環境の取得
16
+
17
+ export function runContext(): RunContext {
18
+
19
+ if (location.origin + location.pathname === chrome.runtime.getURL('pages/sidebar.html')) {
20
+
3
- ですが、もっと直接的に知るapiがあれば教えてください。
21
+ return 'sidebar';
22
+
23
+ }
24
+
25
+ if (location.href === chrome.runtime.getURL('_generated_background_page.html')) {
26
+
27
+ return 'background';
28
+
29
+ }
30
+
31
+ return 'content';
32
+
33
+ }
34
+
35
+
36
+
37
+ ```