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