Predecision Label

Decision label. Try to take over the world!

当前为 2024-10-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Predecision Label
  3. // @namespace http://baidu.com/
  4. // @version 0.4.5
  5. // @description Decision label. Try to take over the world!
  6. // @author You
  7. // @match http://ov.baidu-int.com/*
  8. // @match http://yf.baidu-int.com/*
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=baidu-int.com
  10. // @require https://code.jquery.com/jquery-3.6.0.min.js
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. let label_list = [];
  18. let cur_label = {};
  19. let ads_id = -1;
  20. let start_pre_seq = -1;
  21. let end_pre_seq = -1;
  22. let cur_pre_seq = -1;
  23. let cur_obs_id = -1
  24. let is_label_panel_show = false;
  25. let old_info;
  26. var initialMouseX, initialMouseY, initialElementX, initialElementY;
  27. function set_cur_info() {
  28. var _ads_id = parseInt($("section").children('p').children('span')[1].textContent);
  29. var _cur_pre_seq = parseInt($(".seq-wrap").text().replace('PlanSeq:', ""));
  30. var _cur_obs_id = parseInt($("div.viz-detail-text-wrapper > span").text().replace('id:', ""));
  31. if (window.location.host == 'yf.baidu-int.com') {
  32. _ads_id = getUrlParam("ads_id");
  33. if (_ads_id != null) {
  34. _ads_id = parseInt(_ads_id);
  35. } else {
  36. _ads_id = -1;
  37. }
  38. var s = $('#dreamview-container > div > div > div.pannel.react-draggable > div.context > div.dashboard.inner-box.scrollHeightL > table > tbody > tr:nth-child(3) > td:nth-child(2) > p:nth-child(3) > span')[0];
  39. _cur_pre_seq = parseInt(s.textContent.replace('PRE:', ""));
  40. }
  41. ads_id = isNaN(_ads_id)? ads_id: _ads_id;
  42. cur_pre_seq = isNaN(_cur_pre_seq)? cur_pre_seq: _cur_pre_seq;
  43. cur_obs_id = isNaN(_cur_obs_id)? cur_obs_id: _cur_obs_id;
  44. $('#ads_id').text(ads_id);
  45. $('#seq_num').text(cur_pre_seq);
  46. $('#obs_id').text(cur_obs_id);
  47. cur_label.ads_id = ads_id;
  48. console.log(ads_id);
  49. console.log(cur_pre_seq);
  50. console.log(cur_obs_id);
  51. }
  52. function getUrlParam(name)
  53. {
  54. var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  55. var value = window.location.search.substr(1).match(reg);
  56. if (value != null) return unescape(value[2]); return null;
  57. }
  58. function setStartSeq() {
  59. set_cur_info();
  60. cur_label.start_seq = cur_pre_seq;
  61. console.log("set start seq", cur_label.start_seq);
  62. }
  63. function setEndSeq() {
  64. set_cur_info();
  65. cur_label.end_seq = cur_pre_seq;
  66. console.log("set end seq", cur_label.end_seq);
  67. }
  68. //<textarea id="decision_label_show_list" placeholder="" rows="6" style="width:100%;background-color: #292f42;padding: 10px;max-width: 100%;max-height: 100%;line-height: 1.5;border-radius: 5px;border: 1px solid #ccc;"></textarea>
  69. function showLabelList() {
  70. $('#decision_label_show_list').empty();
  71. for (let i = 0; i < label_list.length; i++){
  72. var label = label_list[i];
  73. var str = label.ads_id + '\t' + label.obs_id + '\t' + label.start_seq + '\t' + label.end_seq + '\t' + label.dec_label;
  74. var text_ele = $('<textarea>', {
  75. id: `label${i}`,
  76. text: str,
  77. placeholder: "",
  78. rows: "1",
  79. style: "width:80%;background-color: #008844;margin-left: 5px;margin-right:5px;margin-top: 5px;"
  80. });
  81. $(`label${i}`).on('input', function(){
  82. this.styel.height = 'auto';
  83. this.style.height = (this.scrollHeight) + 'px';
  84. });
  85. var button_ele = $("<button>", {
  86. text: "删除",
  87. style: "background-color: #008844;margin-left: 10px;margin-right:5px;margin-top: 5px;"
  88. });
  89. button_ele.click(function(){
  90. if (i >= 0 && i < label_list.length) {
  91. label_list.splice(i, 1);
  92. console.log("删除" + toString(i));
  93. showLabelList();
  94. }
  95. });
  96. var div = $('<div>', {
  97. style: "display: flex;"
  98. })
  99. // div.append(text_ele);
  100. div.append(text_ele).append(button_ele);
  101. $('#decision_label_show_list').append(div);
  102. console.log("add show string", str);
  103. }
  104. }
  105. function checkOverlaps(labels, cur_label) {
  106. for (let i = 0; i < labels.length; i++) {
  107. var label = labels[i];
  108. if (cur_label.obs_id != label.obs_id){
  109. continue;
  110. }
  111. // 检查重叠:如果一个区间的开始在另一个区间的开始和结束之间,或者反之亦然
  112. if ((cur_label.start_seq < label.end_seq && cur_label.end_seq > label.start_seq) ||
  113. (label.start_seq < cur_label.end_seq && label.end_seq > cur_label.start_seq)) {
  114. if (cur_label.label != label.dec_label) {
  115. console.log(`障碍物${cur_label.obs_id}与第${i}个标签冲突`);
  116. alert(`障碍物${cur_label.obs_id}与第${i}个标签冲突`);
  117. return true;
  118. } else {
  119. console.log(`障碍物${label.obs_id}区间重复标注`);
  120. console.warn();(`障碍物${label.obs_id}区间重复标注`);
  121. return false;
  122. }
  123. }
  124. }
  125. return false; // 没有重叠
  126. }
  127. function onLabel(dec_label){
  128. $('#label_btn_' + dec_label.toString()).click(function() {
  129. // 添加类以实现按钮变亮效果
  130. $('#label_btn_' + dec_label.toString()).addClass('clicked');
  131. // 在 300 毫秒后移除类以恢复按钮原始状态
  132. setTimeout(function() {
  133. $('#label_btn_' + dec_label.toString()).removeClass('clicked');
  134. }, 300);
  135. });
  136. if (isNaN(ads_id) || isNaN(cur_obs_id) || isNaN(cur_label.start_seq) || isNaN(cur_label.end_seq) || isNaN(dec_label)) {
  137. return;
  138. }
  139. if (ads_id <= 0 && getUrlParam("ads_id") != null) {
  140. alert("ads id 不合法:" + ads_id.toString());
  141. return;
  142. }
  143. if (cur_obs_id <= 0) {
  144. alert("obs id 不合法:" + cur_obs_id.toString());
  145. return;
  146. }
  147. if (dec_label < 0) {
  148. alert("label 不合法" + dec_label.toString());
  149. return;
  150. }
  151. cur_label.ads_id = ads_id;
  152. cur_label.obs_id = cur_obs_id;
  153. cur_label.dec_label = dec_label;
  154. console.log('add decision label to list', cur_label);
  155. if (checkOverlaps(label_list, cur_label)) {
  156. return;
  157. }
  158. label_list.push(cur_label);
  159. showLabelList();
  160. cur_label = {};
  161. }
  162. function showDecisionLabelPanel() {
  163. $("#decision_label_panel").css('z-index', -1 * $("#decision_label_panel").css('z-index'));
  164. console.log("move decision label panel to z index ", $("#decision_label_panel").css('z-index'));
  165. }
  166. // Your code here...
  167. var label_tab = `
  168. <button type="button" onclick="showDecisionLabelPanel()" class="ant-btn ant-btn-default ant-btn-sm" style="margin-right: 10px;">
  169. <div class="ant-space ant-space-horizontal ant-space-align-center" style="gap: 8px;">
  170. <div class="ant-space-item">
  171. 决策标注
  172. </div>
  173. </div>
  174. </button>
  175. `;
  176. var label_panel_html =`
  177. <div class="container-fluid" id="decision_label_panel" style="position: absolute; top: 55%; right: 20px;width: 500px;height:30%;background-color: #292f42;z-index: -9999;">
  178. <div class="col-md-2 column" style="float: center;">
  179. </div>
  180. <div class="row clearfix">
  181. <div class="col-md-2 column" style="float: center;">
  182. <div class="row clearfix">
  183. <label>
  184. ads_id:
  185. </label>
  186. <label id='ads_id'>
  187. -1
  188. </label>
  189. <label>
  190. obs_id:
  191. </label>
  192. <label id='obs_id'>
  193. -1
  194. </label>
  195. <label>
  196. seq_num:
  197. </label>
  198. <label id='seq_num'>
  199. -1
  200. </label>
  201. </div>
  202. </div>
  203. <div class="col-md-2 column">
  204. <div class="row clearfix" style="float: center;">
  205. <button style="width:45%;margin-left: 3%;background-color: red;transition: background-color 0.3s ease;" onclick="setStartSeq()">
  206. seq_start
  207. </button>
  208. <button style="width:45%;margin-left: 2%;background-color: green;" onclick="setEndSeq()">
  209. seq_end
  210. </button>
  211. </div>
  212. <br>
  213. <div class="btn-group" style="margin-left: 2%">
  214. <button style="background-color: #8C0044;margin-left: 5px;margin-top: 5px;" id="label_btn_5" type="button" value="5" onclick="onLabel(5)">
  215. Yield(5)
  216. </button>
  217. <button style="background-color: #886600;margin-left: 5px;margin-top: 5px;" id="label_btn_2" type="button" value="2" onclick="onLabel(2)">
  218. Overtake(2)
  219. </button>
  220. </div>
  221. <br>
  222. <div>
  223. <div id="decision_label_show_list" style="width:100%">
  224. </div>
  225. </div>
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. `;
  231. function clearLabel() {
  232. return;
  233. $('#decision_label_show_list').val('');
  234. label_list = [];
  235. }
  236. function onMouseMove(event) {
  237. // 计算鼠标偏移量
  238. var offsetX = event.clientX - initialMouseX;
  239. var offsetY = event.clientY - initialMouseY;
  240. // 将偏移量应用于元素的位置
  241. $('#decision_label_panel').css({
  242. left: initialElementX + offsetX + 'px',
  243. top: initialElementY + offsetY + 'px'
  244. });
  245. }
  246. // 鼠标释放事件处理程序
  247. function onMouseUp() {
  248. // 移除事件监听器
  249. $(document).off('mousemove', onMouseMove);
  250. $(document).off('mouseup', onMouseUp);
  251. }
  252. window.onMouseMove = onMouseMove;
  253. window.onMouseUp = onMouseUp
  254. window.set_cur_info = set_cur_info;
  255. window.showDecisionLabelPanel = showDecisionLabelPanel;
  256. window.setStartSeq = setStartSeq;
  257. window.setEndSeq = setEndSeq;
  258. window.onLabel = onLabel;
  259. window.clearLabel = clearLabel;
  260. window.showLabelList = showLabelList;
  261. setTimeout(function() {
  262. console.log("3s time out");
  263. $(".ov-header-operations").prepend(label_tab);
  264. $("body").append(label_panel_html);
  265. $('#decision_label_panel').mousedown(function(event) {
  266. var elementTop = $('#decision_label_panel').offset().top;
  267. var mouseY = event.clientY;
  268. console.log('elementTop', elementTop, 'mouseY', mouseY);
  269. if (mouseY > elementTop + 10) {
  270. return; // 如果不在顶部,则忽略拖动
  271. }
  272. // 记录鼠标位置和元素初始位置
  273. initialMouseX = event.clientX;
  274. initialMouseY = event.clientY;
  275. initialElementX = $('#decision_label_panel').offset().left;
  276. initialElementY = $('#decision_label_panel').offset().top;
  277. console.log("initialMouseX", initialMouseX);
  278. // 添加事件监听器
  279. $(document).mousemove(onMouseMove);
  280. $(document).mouseup(onMouseUp);
  281. });
  282. }, 3000); // 等待3秒
  283. })();