|
| 1 | +var metaData = {}; |
| 2 | + |
| 3 | +fetch("./data.json") |
| 4 | + .then(Response => Response.json()) |
| 5 | + .then(data => { |
| 6 | + metaData = data; |
| 7 | + }); |
| 8 | + |
| 9 | +const browserAppData = chrome || browser; |
| 10 | + |
| 11 | +var idx = 0; |
| 12 | +var recorded_actions = []; |
| 13 | +var action_name_convert = { |
| 14 | + type: "text", |
| 15 | + open: "go to link", |
| 16 | + Go_to_link: "go to link", |
| 17 | + doubleClick: "double click", |
| 18 | + Validate_Text: "validate full text", |
| 19 | + Validate_Text_By_AI: "validate full text by ai", |
| 20 | + Save_Text: "save attribute", |
| 21 | + Wait_For_Element_To_Appear: "wait", |
| 22 | + Wait_For_Element_To_Disappear: "wait disable", |
| 23 | +} |
| 24 | + |
| 25 | +async function start_recording(){ |
| 26 | + idx = 0; |
| 27 | + recorded_actions = []; |
| 28 | +} |
| 29 | +async function stop_recording(){ |
| 30 | + // When there are 2 iframes. it saves 3 times. this is a temporary fix. Should be fixed properly |
| 31 | + if (recorded_actions.length > 0) |
| 32 | + browserAppData.storage.local.set({ |
| 33 | + recorded_actions: recorded_actions, |
| 34 | + }).then(()=>{ |
| 35 | + idx = 0; |
| 36 | + recorded_actions = []; |
| 37 | + }); |
| 38 | +} |
| 39 | +async function fetchAIData(idx, command, value, url, document){ |
| 40 | + if (command === 'go to link'){ |
| 41 | + let go_to_link = { |
| 42 | + action: 'go to link', |
| 43 | + data_list: [url], |
| 44 | + element: "", |
| 45 | + is_disable: false, |
| 46 | + name: `Open ${(url.length>25) ? url.slice(0,20) + '...' : url}`, |
| 47 | + value: url, |
| 48 | + main: [['go to link', 'selenium action', url]], |
| 49 | + xpath: "", |
| 50 | + }; |
| 51 | + recorded_actions[idx] = go_to_link; |
| 52 | + console.log(recorded_actions); |
| 53 | + browserAppData.storage.local.set({ |
| 54 | + recorded_actions: recorded_actions, |
| 55 | + }) |
| 56 | + return; |
| 57 | + } |
| 58 | + recorded_actions[idx] = 'empty'; |
| 59 | + browserAppData.storage.local.set({ |
| 60 | + recorded_actions: recorded_actions, |
| 61 | + }) |
| 62 | + let validate_full_text_by_ai = false |
| 63 | + if (command === 'validate full text by ai'){ |
| 64 | + command = 'validate full text'; |
| 65 | + validate_full_text_by_ai = true; |
| 66 | + } |
| 67 | + |
| 68 | + var dataj = { |
| 69 | + "page_src": document, |
| 70 | + "action_name": command, |
| 71 | + "action_type": "selenium", |
| 72 | + "action_value": value, |
| 73 | + "source": "web", |
| 74 | + }; |
| 75 | + var data = JSON.stringify(dataj); |
| 76 | + |
| 77 | + const url_ = `${metaData.url}/ai_record_single_action/` |
| 78 | + const input = { |
| 79 | + method: "POST", |
| 80 | + headers: { |
| 81 | + // "Content-Type": "application/json", |
| 82 | + "X-Api-Key": metaData.apiKey, |
| 83 | + }, |
| 84 | + body: data, |
| 85 | + } |
| 86 | + var r = await fetch(url_, input) |
| 87 | + var resp = await r.json(); |
| 88 | + let response = resp.ai_choices; |
| 89 | + |
| 90 | + if (validate_full_text_by_ai){ |
| 91 | + let text_classifier = await browserAppData.runtime.sendMessage({ |
| 92 | + action: 'content_classify', |
| 93 | + text: value, |
| 94 | + }); |
| 95 | + |
| 96 | + console.log("text_classifier", text_classifier); |
| 97 | + let label = text_classifier[0].label; |
| 98 | + label = label.charAt(0).toUpperCase() + label.slice(1).toLowerCase(); |
| 99 | + let offset = Number((text_classifier[0].score * 0.9).toFixed(2)); |
| 100 | + offset = Math.max(0.8, offset); |
| 101 | + response[0].data_set = response[0].data_set.slice(0,-1) |
| 102 | + .concat([[label, "text classifier offset", offset]]) |
| 103 | + .concat(response[0].data_set.slice(-1)) |
| 104 | + value = ''; |
| 105 | + } |
| 106 | + else if (command === 'save attribute'){ |
| 107 | + response[0].data_set = response[0].data_set.slice(0,-1) |
| 108 | + .concat([ |
| 109 | + ["text", "save parameter", "var_name"], |
| 110 | + ["save attribute", "selenium action", "save attribute"], |
| 111 | + ]) |
| 112 | + value = ''; |
| 113 | + } |
| 114 | + else if (['wait', 'wait disable'].includes(command)){ |
| 115 | + value = 10; |
| 116 | + } |
| 117 | + response[0].short.value = value; |
| 118 | + if (value) response[0].data_set[response[0].data_set.length-1][response[0].data_set[0].length-1] = value; |
| 119 | + recorded_actions[idx] = { |
| 120 | + action: response[0].short.action, |
| 121 | + data_list: [response[0].short.value], |
| 122 | + element: response[0].short.element, |
| 123 | + is_disable: false, |
| 124 | + name: response[0].name, |
| 125 | + value: response[0].short.value, |
| 126 | + main: response[0].data_set, |
| 127 | + xpath: response[0].xpath, |
| 128 | + }; |
| 129 | + console.log(recorded_actions); |
| 130 | + browserAppData.storage.local.set({ |
| 131 | + recorded_actions: recorded_actions, |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +async function record_action(command, value, url, document){ |
| 136 | + if (Object.keys(action_name_convert).includes(command)) command = action_name_convert[command] |
| 137 | + console.log("... Action recorder start"); |
| 138 | + idx += 1; |
| 139 | + if (recorded_actions.length === 0 || recorded_actions.length > 0 && recorded_actions[0].action != 'go to link'){ |
| 140 | + let go_to_link = { |
| 141 | + action: 'go to link', |
| 142 | + data_list: [url], |
| 143 | + element: "", |
| 144 | + is_disable: false, |
| 145 | + name: `Open ${(url.length>25) ? url.slice(0,20) + '...' : url}`, |
| 146 | + value: url, |
| 147 | + main: [['go to link', 'selenium action', url]], |
| 148 | + xpath: "", |
| 149 | + }; |
| 150 | + if (recorded_actions.length === 0) recorded_actions[0] = go_to_link; |
| 151 | + else recorded_actions.unshift(go_to_link); |
| 152 | + idx += 1; |
| 153 | + } |
| 154 | + fetchAIData(idx-1, command, value, url, document); |
| 155 | +} |
| 156 | +browserAppData.runtime.onMessage.addListener( |
| 157 | + function(request, sender, sendResponse) { |
| 158 | + if (request.apiName == 'start_recording') { |
| 159 | + start_recording(); |
| 160 | + } |
| 161 | + else if (request.apiName == 'record_action') { |
| 162 | + record_action( |
| 163 | + request.command, |
| 164 | + // request.target, |
| 165 | + request.value, |
| 166 | + request.url, |
| 167 | + request.document, |
| 168 | + ); |
| 169 | + } |
| 170 | + else if (request.apiName == 'stop_recording') { |
| 171 | + stop_recording(); |
| 172 | + } |
| 173 | + } |
| 174 | +); |
0 commit comments