回答編集履歴

1

追記

2018/05/21 22:46

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -5,3 +5,65 @@
5
5
 
6
6
 
7
7
  `tabID`は`chrome.runtime.onMessage.addListener`の引数`sender`を`sender.tab.id`の形でも取得できます。
8
+
9
+
10
+
11
+ ---
12
+
13
+ コンパイル通してないですが、手元の開発中のChrome拡張のソースからコピペするとこんな感じでしょうか。
14
+
15
+ ```JavaScript
16
+
17
+ chrome.contextMenus.create({
18
+
19
+ "title": "校正チェック",
20
+
21
+ "type": "normal",
22
+
23
+ "contexts": ["all"],
24
+
25
+ "onclick": function(info) {
26
+
27
+ chrome.tabs.getSelected(null, function(tab) {
28
+
29
+ // ★現在選択中のtab.idが必要なので、getSelectedメソッドの中にsendRequestを記述する。
30
+
31
+ alert("step1");
32
+
33
+ send_message(tab.id);
34
+
35
+ alert("step1");
36
+
37
+ });
38
+
39
+ }
40
+
41
+ });
42
+
43
+
44
+
45
+ function send_message(tabid){
46
+
47
+ chrome.tabs.sendMessage(tabid, {"action": "getName"}, (response) => {
48
+
49
+ console.log('ui');
50
+
51
+ });
52
+
53
+ }
54
+
55
+ ```
56
+
57
+ content_script側
58
+
59
+ ```JavaScript
60
+
61
+ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
62
+
63
+ // やりたいこと。
64
+
65
+ return true;
66
+
67
+ });
68
+
69
+ ```