script-aaoDraggable

Add possibility to move AAO simply by dragging it

  1. // ==UserScript==
  2. // @name script-aaoDraggable
  3. // @namespace https://github.com/Poul0s/script-aooDraggable
  4. // @version 2025-03-02.01
  5. // @description Add possibility to move AAO simply by dragging it
  6. // @author Thunlos
  7. // @match https://www.operateur112.fr/
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=operateur112.fr
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14. 'use strict';
  15. const iframeLink = window.location.origin + "/aaos";
  16.  
  17. function startAAODrag(event, aao, iframeContentWindow) {
  18. aao.style.cursor = "grabbing";
  19.  
  20. let currentHoveringColumn = null;
  21. let dummy = null;
  22. let currentColumn = aao.parentElement;
  23.  
  24. function removeHoveringColumn() {
  25. if (!currentHoveringColumn) return;
  26. currentHoveringColumn = null;
  27. dummy.remove();
  28. }
  29.  
  30. function drag(event) {
  31. if (event.clientX < 0 || event.clientY < 0) {
  32. return;
  33. }
  34. let elements = iframeContentWindow.document.elementsFromPoint(event.clientX, event.clientY);
  35. let categorySwitcher = elements.find(e => e.href && e.href.startsWith(iframeLink + "#aao_category_"));
  36. if (categorySwitcher && !categorySwitcher.parentElement.classList.contains("active")) {
  37. categorySwitcher.click();
  38. } else {
  39. let categoryContainer = elements.find(e => e.id && e.id.startsWith("aao_category_"));
  40. if (categoryContainer) {
  41. let columns = categoryContainer.querySelectorAll(".col-sm-2.col-xs-4");
  42. let hoveringColumn = Array.from(columns).find(c => {
  43. let rect = c.getBoundingClientRect();
  44. return rect.left < event.clientX && rect.right > event.clientX
  45. });
  46. if (hoveringColumn && hoveringColumn != currentColumn) {
  47. if (hoveringColumn != currentHoveringColumn) {
  48. removeHoveringColumn();
  49. currentHoveringColumn = hoveringColumn;
  50. dummy = aao.cloneNode(true);
  51. dummy.style.opacity = 0.5;
  52. currentHoveringColumn.appendChild(dummy);
  53. }
  54. } else
  55. removeHoveringColumn();
  56. } else
  57. removeHoveringColumn();
  58. }
  59.  
  60. }
  61.  
  62. function dragend(event) {
  63. if (currentHoveringColumn) {
  64. let editLink = aao.dataset.editLink;
  65. let columnNumber = Array.prototype.indexOf.call(currentHoveringColumn.parentNode.children, currentHoveringColumn) + 1;
  66. let currentCategory = iframeContentWindow.document.querySelector("#aao-tabs li[role='presentation'].active");
  67. if (!currentCategory) {
  68. console.error("No active category found");
  69. return;
  70. }
  71. let categoryName = currentCategory.firstElementChild.innerText;
  72. let iframe = iframeContentWindow.document.createElement("iframe");
  73. iframe.style.visibility = "hidden";
  74. iframe.style.position = "absolute";
  75. iframe.style.width = "1px";
  76. iframe.style.height = "1px";
  77. iframeContentWindow.document.body.appendChild(iframe);
  78. iframe.src = editLink;
  79. let loaded = false;
  80. iframe.onload = () => {
  81. if (loaded) return;
  82. loaded = true;
  83. let editContentWindow = iframe.contentWindow;
  84. let column_nb_select = editContentWindow.document.getElementById("aao_column_number");
  85. column_nb_select.value = columnNumber.toString();
  86. let category_select = editContentWindow.document.getElementById("aao_category_id");
  87. if (category_select === null)
  88. category_select = editContentWindow.document.getElementById("aao_aao_category_id");
  89. let option = null
  90. for (let opt of category_select.options) {
  91. if (opt.innerText == categoryName) {
  92. option = opt;
  93. break;
  94. }
  95. }
  96. if (!option) {
  97. console.error("No category found");
  98. return;
  99. }
  100. category_select.value = option.value;
  101. editContentWindow.document.getElementById("save-button").click();
  102. dummy.style.opacity = 1;
  103. dummy.addEventListener("dragstart", (event) => startAAODrag(event, dummy, iframeContentWindow));
  104. if (aao.nextElementSibling && aao.nextElementSibling.nodeName == "BR") {
  105. aao.nextElementSibling.remove();
  106. }
  107. aao.remove();
  108. setTimeout(() => {
  109. iframe.remove();
  110. }, 5000);
  111. }
  112. }
  113. aao.removeEventListener("drag", drag);
  114. aao.removeEventListener("dragend", dragend);
  115. }
  116.  
  117. aao.addEventListener("drag", drag);
  118. aao.addEventListener("dragend", dragend);
  119. }
  120. function setAAOsDraggables(iframeNode, iframeContentWindow) {
  121. let aaos = iframeContentWindow.document.getElementsByClassName("aao_btn_group");
  122. for (let aao of aaos) {
  123. aao.setAttribute("draggable", "true");
  124. aao.dataset.editLink = aao.firstElementChild.href;
  125. aao.firstElementChild.removeAttribute("href");
  126. aao.firstElementChild.style.cursor = "grab";
  127. aao.addEventListener("dragstart", (event) => startAAODrag(event, aao, iframeContentWindow));
  128. }
  129. }
  130.  
  131.  
  132. let popupPage = document.getElementById("lightbox_box");
  133. const callback = (mutationList, observer) => {
  134. for (const mutation of mutationList) {
  135. if (mutation.type === "childList") {
  136. for (let node of mutation.addedNodes) {
  137. if (node.classList.contains("lightbox_iframe")) {
  138. node.addEventListener("load", () => {
  139. if (node.src.startsWith(iframeLink)) {
  140. setAAOsDraggables(node, node.contentWindow);
  141. }
  142. })
  143. }
  144. }
  145. }
  146. }
  147. };
  148.  
  149. const observer = new MutationObserver(callback)
  150. observer.observe(popupPage, { attributes: false, childList: true, subtree: false });
  151. })();