Advertisement
Guest User

Ozbargains Tampermonkey Script v0.1

a guest
Feb 22nd, 2024
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Ozbargains Live helper
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Keep track of what you last viewed on ozbargains
  6. // @author       T. Coleman
  7. // @match        https://www.ozbargain.com.au/live
  8. // @icon         https://www.google.com/s2/favicons?sz=64&domain=atomicobject.com
  9. // @grant        GM_addStyle
  10. // ==/UserScript==
  11.  
  12. // Copyright 2024 T. Coleman
  13.  
  14. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  15. // associated documentation files (the “Software”), to deal in the Software without restriction,
  16. // including without limitation the rights to use, copy, modify, merge, publish, distribute,
  17. // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  18. // furnished to do so, subject to the following conditions:
  19.  
  20. // The above copyright notice and this permission notice shall be included in all copies or
  21. // substantial portions of the Software.
  22.  
  23. // THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  24. // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  26. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28.  
  29. const resetTimeoutDelay = 5 * 60 * 1000; // 5 minute auto reset time
  30. const controlBarID = "checks";
  31.  
  32. (function() {
  33.     'use strict';
  34.  
  35.     const inlineCSS =
  36. `table#livetable tr.bookmark td:nth-child(2) {
  37.     width: 100%;
  38.     height: 10px;
  39. }
  40. #livetable tr.bookmark td:nth-child(1) {
  41.     display: none;
  42. }
  43. tr.bookmark td {
  44.     background-color: #d6ffd6;
  45. }
  46.  
  47. .templatecontainer {
  48.     display: contents;
  49. }
  50.  
  51. .customButton {
  52.     background-color: #d6ffd6;
  53.     border: 1px solid #72f172;
  54.     padding: 5px;
  55.     user-select: none;
  56. }
  57.  
  58. .customButton:hover {
  59.         background-color: #afffd2;
  60. }
  61.  
  62. .customButton.paused {
  63.         background-color: #A5FFA5;
  64. }
  65.  
  66. .customButton.paused:hover {
  67.         background-color: #89FF89;
  68. }
  69.  
  70.  
  71.  
  72. .hidden {
  73.     display: none;
  74. }
  75.  
  76. .customButton:hover {
  77.     background-color: #afffd2;
  78. }
  79.  
  80. `;
  81.  
  82.   const controlsTemplate =
  83. `<li>
  84.   <div id="BookmarkPause" class="customButton">Pause</div>
  85. </li>
  86. <li>
  87.   <div id="BookmarkReset" class="customButton">Reset</div>
  88. </li>
  89. `;
  90.  
  91.  
  92.   const resetBtnId = "BookmarkReset";
  93.   const pauseBtnId = "BookmarkPause";
  94.  
  95.   GM_addStyle(inlineCSS);
  96.  
  97.   // inject controls html
  98.   const ctrlBar = document.getElementById(controlBarID);
  99.  
  100.   ctrlBar && (ctrlBar.innerHTML += controlsTemplate);
  101.  
  102.   const resetBtn = document.getElementById(resetBtnId);
  103.   const pauseBtn = document.getElementById(pauseBtnId);
  104.  
  105.   const dragDrop = function(ev) {
  106.     // console.log("drag ended");
  107.     ev.stopPropagation();
  108.     if (drgSrc) {
  109.       this.after(drgSrc);
  110.       drgSrc = undefined;
  111.     }
  112.   }
  113.  
  114.   const dragOver = function(ev) {
  115.     ev.preventDefault();
  116.   }
  117.  
  118.   const dragStart = function(ev) {
  119.     ev.dataTransfer.effectAllowed = "move";
  120.     drgSrc = this;
  121.  
  122.     const rows = document.querySelectorAll("table#livetable tbody tr.type-deal");
  123.     // console.log(`Drag started: Found ${rows.length} rows`);
  124.     for (const r of rows) {
  125.       r.ondrop = dragDrop;
  126.       r.ondragover = dragOver;
  127.     }
  128.   }
  129.  
  130.   const dragEnter = function(ev) {
  131.    
  132.   }
  133.  
  134.   const injectRow = function() {
  135.     const tbody = document.querySelector("table#livetable tbody");
  136.  
  137.     const newbar = document.createElement("tr");
  138.     const cell1 = document.createElement("td");
  139.     const cell2 = document.createElement("td");
  140.     cell2.setAttribute("colspan","5");
  141.  
  142.     newbar.classList.add("bookmark");
  143.     newbar.appendChild(cell1);
  144.     newbar.appendChild(cell2);
  145.     newbar.setAttribute("draggable","true");
  146.     newbar.ondragstart = dragStart;
  147.     tbody.prepend(newbar);
  148.  
  149.     return newbar;
  150.   }
  151.  
  152.   let schedReset = undefined;
  153.   let paused = false;
  154.   let drgSrc = undefined;
  155.  
  156.   const removeMarkersExcept = function(except) {
  157.     const tbody = document.querySelector("table#livetable tbody");
  158.     const markers = tbody.querySelectorAll("tr.bookmark");
  159.  
  160.     markers.forEach(e => e === except || tbody.removeChild(e));
  161.   }
  162.  
  163.   let pendRow = undefined;
  164.  
  165.  
  166.   const activateReset = function() {
  167.     removeMarkersExcept(pendRow);
  168.     pendRow.classList.remove("hidden");
  169.     pendRow = undefined;
  170.     schedReset = undefined;
  171.   }
  172.  
  173.   const manualReset = function() {
  174.     if (!pendRow) pendRow = injectRow();
  175.     activateReset();
  176.   }
  177.  
  178.  
  179.   const scheduleReset = function() {
  180.     pendRow = injectRow();
  181.     pendRow.classList.add("hidden");
  182.  
  183.     schedReset = setTimeout(activateReset,resetTimeoutDelay)
  184.   }
  185.  
  186.   const cancelReset = function() {
  187.     if (schedReset != undefined) {
  188.       clearTimeout(schedReset);
  189.       schedReset = undefined;
  190.       pendRow?.parentElement?.removeChild(pendRow);
  191.       pendRow = undefined;
  192.     }
  193.  
  194.   }
  195.  
  196.   const togglePause = function() {
  197.     paused = !paused;
  198.  
  199.     if (paused) {
  200.       pauseBtn?.classList.add("paused");
  201.       cancelReset();
  202.     }
  203.  
  204.     if (!paused) {
  205.       pauseBtn?.classList.remove("paused");
  206.     }
  207.   }
  208.  
  209.  
  210.  
  211.   // const style = document.createElement("style");
  212.   // style.setAttribute("type","text/css");
  213.   // style.innerText=inlineCSS;
  214.   // document.head.appendChild(style);
  215.  
  216.   document.addEventListener("blur",scheduleReset);
  217.   document.addEventListener("focus", cancelReset);
  218.  
  219.   resetBtn?.addEventListener("click", manualReset);
  220.  
  221.   pauseBtn?.addEventListener("click", togglePause);
  222.  
  223. })();
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement