質問編集履歴

1

json追加

2016/12/03 10:13

投稿

usuallyi
usuallyi

スコア41

test CHANGED
@@ -1 +1 @@
1
- chrome extensionwindow.getSelectionが取得できない
1
+ chrome extensionで選択範囲の文字を取得できない、また、新規タブを隣のタブに開けない
test CHANGED
@@ -3,6 +3,18 @@
3
3
  ページ上で文字を選択して、wikipediaで検索して表示させたいのですが、
4
4
 
5
5
  window.getSelectionで文字を取得できずに困っています。
6
+
7
+
8
+
9
+ 検索した際に、右タブに新規タブを開きたいと思い、下記を参考にしたのですが、
10
+
11
+ 以下のエラーが発生してしまいます。
12
+
13
+ Error in event handler for contextMenus: ReferenceError: tab is not defined
14
+
15
+ at onclick
16
+
17
+ http://gihyo.jp/dev/serial/01/chrome-extensions/0001?page=3
6
18
 
7
19
 
8
20
 
@@ -22,12 +34,62 @@
22
34
 
23
35
  var selection = window.getSelection().toString();
24
36
 
37
+ console.log(selection);
38
+
25
39
  var url = "https://ja.wikipedia.org/wiki/"+selection;
26
40
 
41
+ console.log();
42
+
27
- chrome.tabs.create({ url : url});
43
+ chrome.tabs.create({index:tab.index+1 ,url : url,selected:true});
28
44
 
29
45
  }
30
46
 
31
47
  });
32
48
 
33
49
  ```
50
+
51
+ manifest.json
52
+
53
+ ```json
54
+
55
+ {
56
+
57
+ "name": "wiki_search",
58
+
59
+ "version": "1.0.0",
60
+
61
+ "manifest_version": 2,
62
+
63
+ "description": "右クリックメニューからwikipedia検索をする。",
64
+
65
+ "permissions" : [
66
+
67
+ "tabs",
68
+
69
+ "contextMenus",
70
+
71
+ "http://*/*",
72
+
73
+ "https://*/*"
74
+
75
+ ],
76
+
77
+ "icons": {
78
+
79
+ "128": "img/icon_128.png",
80
+
81
+ "48": "img/icon_48.png",
82
+
83
+ "16": "img/icon_16.png"
84
+
85
+ },
86
+
87
+ "background" : {
88
+
89
+ "scripts" : ["background.js"]
90
+
91
+ }
92
+
93
+ }
94
+
95
+ ```