-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
64 lines (50 loc) · 1.35 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
<head>
<title>LinkPreviewサンプル</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
img {
width: 400px;
}
</style>
</head>
<body>
<h3></h3>
<a><img src=""/></a>
<script>
const data = {
key: '**************', //ご自身のAPIキーに置き換えてください
q: 'https://paiza.jp/' //任意のURLで試してみてください
}
//プレビュー表示用の関数
const createIMG = json => {
document.querySelector('img').src = json.image;
document.querySelector('h3').textContent = json.title;
document.querySelector('a').href = json.url;
}
//LinkPreviewを利用した通信処理
fetch('https://api.linkpreview.net', {
method: 'POST',
mode: 'cors',
body: JSON.stringify(data),
})
.then(data => data.json())
.then(json => createIMG(json));
/*
// jQueryの場合
// https://code.jquery.com/jquery-3.5.1.min.js
$.ajax({
url: "https://api.linkpreview.net",
// dataType: "jsonp",
data: {q:'https://paiza.jp', key: '***************'},
success: function (response) {
console.log(response);
}
});
*/
</script>
</body>
</html>