-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpanel.js
76 lines (62 loc) · 2.4 KB
/
panel.js
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
65
66
67
68
69
70
71
72
73
74
75
76
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// ---- ---- ---- ---- ajax
const sendAjax = {
post: async ({ data, url }) => {
const xhr = new XMLHttpRequest;
xhr.responseType = 'json';
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(data);
const response = await new Promise((res, rej) => {
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
res(xhr.response);
return;
}
};
});
return response;
},
get: async ({ data, url }) => {
const queryString = Object.keys(data).map(key => `${key}=${data[key]}`).join('&');
const xhr = new XMLHttpRequest;
xhr.responseType = 'json';
xhr.open('GET', `${url}?${queryString}`);
xhr.send(null);
const response = await new Promise((res, rej) => {
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
res(xhr.response);
return;
}
};
});
return response;
}
};
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// ---- ---- ---- ---- channel
// 与当前页面的DevTool Page之间建立一个channel
const channel = chrome.runtime.connect(null, {
name: chrome.devtools.inspectedWindow.tabId.toString(),
});
// 监听channel消息
channel.onMessage.addListener(result => {
const { isSuccess, data, mssage } = result;
if (!isSuccess) {
document.querySelector('#error').innerHTML += mesage;
return;
}
const { method, queryString, url, response } = data;
document.querySelector('#result').innerHTML += url + '<br>';
});
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
// ---- ---- ---- ---- Panel页面中的事件
// 点击按钮,模拟发起ajax请求
document.querySelector('#button1').addEventListener('click', async () => {
const urls = document.querySelector('#result').innerHTML.split('<br>');
alert(JSON.stringify(urls, null, 4));
// todo: 这里可以用sendAjax.get或者sendAjax.post发起ajax请求
// 从而将以上保存的http url存到服务器端
});
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----