|
1 |
| -(function ($) { |
2 |
| - if (!$) { |
3 |
| - throw new Error('jQuery 插件加载失败将无法校验机器人配置,但不影响正常使用') |
4 |
| - } |
5 |
| - |
6 |
| - $(function () { |
7 |
| - $(document).on('click', '.robot-config-validate-btn', validateRobotConfig) |
8 |
| - }) |
9 |
| - |
10 |
| - function applyErrorMessage(elt, rsp) { |
11 |
| - if (rsp.status == 200) { |
12 |
| - elt.innerHTML = rsp.responseText |
13 |
| - } else { |
14 |
| - var id = 'valerr' + (iota++) |
15 |
| - elt.innerHTML = '<a href="" onclick="document.getElementById(\'' + id |
16 |
| - + '\').style.display=\'block\';return false">ERROR</a><div id="' |
17 |
| - + id + '" style="display:none">' + rsp.responseText + '</div>' |
18 |
| - var error = document.getElementById('error-description') // cf. oops.jelly |
19 |
| - if (error) { |
20 |
| - var div = document.getElementById(id) |
21 |
| - while (div.firstElementChild) { |
22 |
| - div.removeChild(div.firstElementChild) |
23 |
| - } |
24 |
| - div.appendChild(error) |
| 1 | +function applyErrorMessage(elt, rsp) { |
| 2 | + if (rsp.status == 200) { |
| 3 | + elt.innerHTML = rsp.responseText |
| 4 | + } else { |
| 5 | + var id = 'valerr' + (iota++) |
| 6 | + elt.innerHTML = '<a href="" onclick="document.getElementById(\'' + id |
| 7 | + + '\').style.display=\'block\';return false">ERROR</a><div id="' |
| 8 | + + id + '" style="display:none">' + rsp.responseText + '</div>' |
| 9 | + var error = document.getElementById('error-description') // cf. oops.jelly |
| 10 | + if (error) { |
| 11 | + var div = document.getElementById(id) |
| 12 | + while (div.firstElementChild) { |
| 13 | + div.removeChild(div.firstElementChild) |
25 | 14 | }
|
| 15 | + div.appendChild(error) |
26 | 16 | }
|
27 |
| - Behaviour.applySubtree(elt) |
28 | 17 | }
|
| 18 | + Behaviour.applySubtree(elt) |
| 19 | +} |
29 | 20 |
|
30 |
| - async function validateRobotConfig() { |
31 |
| - var $btn = $(this) |
| 21 | +async function validateRobotConfig(btn) { |
| 22 | + var checkUrl = btn.dataset['validateButtonDescriptorUrl'] + |
| 23 | + '/' + |
| 24 | + btn.dataset['validateButtonMethod'] |
32 | 25 |
|
33 |
| - var checkUrl = $btn.attr('data-validate-button-descriptor-url') + |
34 |
| - '/' + |
35 |
| - $btn.attr('data-validate-button-method') |
| 26 | + var $robot = btn.closest('.robot-config-container') |
| 27 | + var $msg = $robot.querySelector('.robot-config-validate-msg') |
36 | 28 |
|
37 |
| - var $robot = $btn.parents('.robot-config-container') |
38 |
| - var $msg = $robot.find('.robot-config-validate-msg') |
| 29 | + $msg.innerHTML = '' |
39 | 30 |
|
40 |
| - $msg.empty() |
| 31 | + var url = new URL(checkUrl, window.location.origin) |
| 32 | + getParameters($robot).forEach(function (v, k) { |
| 33 | + url.searchParams.set(k, v) |
| 34 | + }) |
41 | 35 |
|
42 |
| - var url = new URL(checkUrl, window.location.origin) |
43 |
| - getParameters($robot).forEach(function (v, k) { |
44 |
| - url.searchParams.set(k, v) |
45 |
| - }) |
| 36 | + var res = await fetch(url, { |
| 37 | + method: 'GET' |
| 38 | + }) |
46 | 39 |
|
47 |
| - var res = await fetch(url, { |
48 |
| - method: 'GET' |
49 |
| - }) |
| 40 | + var resText = await res.text() |
| 41 | + applyErrorMessage($msg, { |
| 42 | + status: res.status, |
| 43 | + responseText: resText |
| 44 | + }) |
| 45 | +} |
50 | 46 |
|
51 |
| - var resText = await res.text() |
52 |
| - applyErrorMessage($msg.get(0), { |
53 |
| - status: res.status, |
54 |
| - responseText: resText |
55 |
| - }) |
| 47 | +function getParameters($robot) { |
| 48 | + /** |
| 49 | + * 代理信息 |
| 50 | + */ |
| 51 | + var $proxy = document.querySelector('#dt-proxyConfigContainer') |
| 52 | + var proxyConfig = { |
| 53 | + type: $proxy.querySelector('select[name="type"]').value, |
| 54 | + host: $proxy.querySelector('input[name="host"]').value, |
| 55 | + port: $proxy.querySelector('input[name="port"]').value |
56 | 56 | }
|
57 | 57 |
|
58 |
| - function getParameters($robot) { |
59 |
| - /** |
60 |
| - * 代理信息 |
61 |
| - */ |
62 |
| - var $proxy = $('#proxyConfigContainer') |
63 |
| - var proxyConfig = { |
64 |
| - type: $proxy.find('select[name="type"]').val(), |
65 |
| - host: $proxy.find('input[name="host"]').val(), |
66 |
| - port: $proxy.find('input[name="port"]').val() |
67 |
| - } |
| 58 | + /** |
| 59 | + * 机器人配置 |
| 60 | + */ |
| 61 | + var id = $robot.querySelector('input[name="id"]').value |
| 62 | + var name = $robot.querySelector('input[name="name"]').value |
| 63 | + var webhook = $robot.querySelector('input[name="webhook"]').value |
| 64 | + // 安全策略 |
| 65 | + var securityPolicyConfigs = [] |
68 | 66 |
|
69 |
| - /** |
70 |
| - * 机器人配置 |
71 |
| - */ |
72 |
| - var id = $robot.find('input[name="id"]').val() |
73 |
| - var name = $robot.find('input[name="name"]').val() |
74 |
| - var webhook = $robot.find('input[name="webhook"]').val() |
75 |
| - // 安全策略 |
76 |
| - var securityPolicyConfigs = $.map( |
77 |
| - $robot.find('.security-config-container'), |
78 |
| - function (t) { |
79 |
| - return { |
80 |
| - type: $(t).find('input[name=type]').val(), |
81 |
| - value: $(t).find('input[name=value]').val() |
82 |
| - } |
| 67 | + $robot.querySelectorAll('.dt-security-config-container').forEach( |
| 68 | + function (item) { |
| 69 | + securityPolicyConfigs.push({ |
| 70 | + type: item.querySelector('input[name=type]').value, |
| 71 | + value: item.querySelector('input[name=value]').value |
83 | 72 | })
|
| 73 | + }) |
84 | 74 |
|
85 |
| - var params = new URLSearchParams() |
| 75 | + var params = new URLSearchParams() |
86 | 76 |
|
87 |
| - params.set('id', id) |
88 |
| - params.set('name', name) |
89 |
| - params.set('webhook', webhook) |
90 |
| - params.set('securityPolicyConfigs', JSON.stringify(securityPolicyConfigs)) |
91 |
| - params.set('proxy', JSON.stringify(proxyConfig)) |
| 77 | + params.set('id', id) |
| 78 | + params.set('name', name) |
| 79 | + params.set('webhook', webhook) |
| 80 | + params.set('securityPolicyConfigs', JSON.stringify(securityPolicyConfigs)) |
| 81 | + params.set('proxy', JSON.stringify(proxyConfig)) |
92 | 82 |
|
93 |
| - return params |
94 |
| - } |
95 |
| -})(jQuery3 || jQuery) |
| 83 | + return params |
| 84 | +} |
0 commit comments