質問編集履歴

5

題名修正

2022/02/09 21:01

投稿

m.s.2000
m.s.2000

スコア32

test CHANGED
@@ -1 +1 @@
1
- TradingViewウィジェットの表示株価をjQueryで取得し、位置やフォントサイズを変更して再表示したい
1
+ クロスドメイン(外部サイト)のiframe中の要素をiframe in iframeを構築してjQueryで取得したい
test CHANGED
@@ -2,79 +2,93 @@
2
2
 
3
3
  ### 前提・実現したいこと
4
4
  TradingViewウィジェットの表示株価が小さすぎ位置も悪い為、株価数値を取得して別の位置に再表示したい。
5
+ しかし、クロスドメインのiframeではセキュリティ上、jQueryで要素を取得出来ないとのこと。
6
+ ネット上で親、子、孫に分けて構築すると取得可能とあったのでこれに従いました。
7
+
8
+ 1, 親ファイル(子をiframeで組み込み、自サーバー)
9
+ 2, 子ファイル(孫をiframeで組み込み、自サーバー)
10
+ 3, 孫(TradingViewウィジェット、外部サーバー)
5
11
 
6
12
  ### 発生している問題
7
- jQueryで該当classの株価数値データを取得しようとしたのですが、console.logで取得データを確認すると、nullを取得ししまエラーにってしまう
13
+ jQueryで該当classの株価数値データを取得しようとしたのですが、console.logで取得データを確認すると、データが入っていな
8
14
 
9
15
 
10
- ### エラセージ
16
+ ### タ取得出来なかったアウトプ
11
- Uncaught TypeError: Cannot read properties of null (reading 'textContent')
17
+ MessageEvent {isTrusted: true, data: '', origin: 'https://......
12
18
 
13
19
 
14
20
  ### TradingViewウィジェット URL
15
- https://jp.tradingview.com/widget/market-overview/
21
+ https://jp.tradingview.com/widget/ticker/
22
+
23
+
24
+
16
25
 
17
26
 
18
27
  ### ソースコード
19
28
  ```html
29
+ <!-- ◆parent.html【親】-->
30
+
31
+ <!doctype html>
32
+ <html lang="ja">
33
+ <head>
20
34
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
35
+ </head>
36
+ <body>
21
37
 
38
+ <iframe class="xxx" src="./child.html"></iframe>
22
39
 
23
- //以下TradingViewから提供されているウィジェットコード
24
- <!-- TradingView Widget BEGIN -->
25
- <div class="tradingview-widget-container">
26
- <div class="tradingview-widget-container__widget"></div>
27
- <div class="tradingview-widget-copyright">TradingView提供の<a href="https://jp.tradingview.com/markets/indices/" rel="noopener" target="_blank"><span class="blue-text">指数</span></a></div>
28
- <script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-market-overview.js" async>
29
- {
30
- "colorTheme": "light",
31
- "dateRange": "12M",
32
- "showChart": true,
33
- "locale": "ja",
34
- "largeChartUrl": "",
35
- "isTransparent": false,
36
- "showSymbolLogo": true,
37
- "showFloatingTooltip": false,
38
- "width": "400",
39
- "height": "660",
40
- "plotLineColorGrowing": "rgba(41, 98, 255, 1)",
41
- "plotLineColorFalling": "rgba(41, 98, 255, 1)",
42
- "gridLineColor": "rgba(240, 243, 250, 0)",
43
- "scaleFontColor": "rgba(120, 123, 134, 1)",
44
- "belowLineFillColorGrowing": "rgba(41, 98, 255, 0.12)",
45
- "belowLineFillColorFalling": "rgba(41, 98, 255, 0.12)",
46
- "belowLineFillColorGrowingBottom": "rgba(41, 98, 255, 0)",
47
- "belowLineFillColorFallingBottom": "rgba(41, 98, 255, 0)",
48
- "symbolActiveColor": "rgba(41, 98, 255, 0.12)",
49
- "tabs": [
50
- {
51
- "title": "指数",
52
- "symbols": [
53
- {
54
- "s": "FOREXCOM:SPXUSD",
55
- "d": "S&P 500"
56
- }
57
- ],
58
- "originalTitle": "Indices"
59
- }
60
- ]
61
- }
62
- </script>
63
- </div>
40
+ </body>
64
- <!-- TradingView Widget END -->
41
+ </html>
65
- //TradingViewウィジェット提供コード終わり
66
-
67
42
 
68
43
 
69
44
  <script>
70
- //該当classを指定して数値データを取得 console.log表示
45
+ //子からデータをキャッチ→→コンソール確認
71
- var textMsg = document.getElementById('.tv-widget-watch-list__last, .js-symbol-last, .apply-overflow-tooltip');
46
+ window.addEventListener('message', function(data_0) {
72
- console.log(textMsg.textContent);
47
+ console.log(data_0);
48
+ });
73
49
  </script>
74
50
  ```
75
51
 
76
52
 
77
53
 
78
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-02-06/0836caf7-e34a-4544-a987-1996f3418327.jpeg)
79
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-02-06/615904bd-023f-4221-9c94-751e5583cca4.jpeg)
80
54
 
55
+
56
+ ```html
57
+ <!-- ◆child.html【子】-->
58
+
59
+ <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
60
+
61
+ <!-- TradingView Widget BEGIN -->
62
+ <div class="tradingview-widget-container">
63
+ <div class="tradingview-widget-container__widget"></div>
64
+ <div class="tradingview-widget-copyright">TradingView提供の<a href="https://jp.tradingview.com" rel="noopener" target="_blank"><span class="blue-text">価格</span></a></div>
65
+ <script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-tickers.js" async>
66
+ {
67
+ "symbols": [
68
+ {
69
+ "proName": "FOREXCOM:SPXUSD",
70
+ "title": "S&P500"
71
+ }
72
+ ],
73
+ "colorTheme": "light",
74
+ "isTransparent": false,
75
+ "showSymbolLogo": true,
76
+ "locale": "ja"
77
+ }
78
+ </script>
79
+ </div>
80
+ <!-- TradingView Widget END -->
81
+
82
+
83
+ <script>
84
+ //孫のiframe中の該当classを指定してデータを取得 →→→ postMessageで親に渡す
85
+ var data_0=$('.tv-ticker-item-change__last, .js-symbol-last, .apply-overflow-tooltip').text();
86
+ window.parent.postMessage(data_0, '*');
87
+ </script>
88
+ ```
89
+
90
+
91
+
92
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-02-10/a0f5f24b-7ce5-42e0-b0e3-6867be31e6c4.jpeg)
93
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-02-10/4f9725c0-604f-43ee-ae74-53a358a337c1.jpeg)
94
+

4

題名修正

2022/02/06 14:09

投稿

m.s.2000
m.s.2000

スコア32

test CHANGED
@@ -1 +1 @@
1
- TradingViewウィジェットの表示株価をjQueryで取得したい
1
+ TradingViewウィジェットの表示株価をjQueryで取得し、位置やフォントサイズを変更して再表示したい
test CHANGED
File without changes

3

題名修正

2022/02/06 09:30

投稿

m.s.2000
m.s.2000

スコア32

test CHANGED
@@ -1 +1 @@
1
- TradingViewウィジェットの株価数値をjQueryで取得したい
1
+ TradingViewウィジェットの表示株価をjQueryで取得したい
test CHANGED
File without changes

2

タグの修正

2022/02/06 09:29

投稿

m.s.2000
m.s.2000

スコア32

test CHANGED
@@ -1 +1 @@
1
- TradingViewウィジェットの株価数値を取得したい
1
+ TradingViewウィジェットの株価数値をjQueryで取得したい
test CHANGED
File without changes

1

2022/02/05 21:21

投稿

m.s.2000
m.s.2000

スコア32

test CHANGED
File without changes
test CHANGED
File without changes