GotBricks? for bricklink.com

05/01/2024, 17:09:39

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

Advertisement:

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

Advertisement:

// ==UserScript==
// @name        GotBricks? for bricklink.com
// @namespace   Violentmonkey Scripts
// @match       https://www.bricklink.com/catalogItemInv.asp*
// @grant       none
// @version     1.01
// @author      Samantha Finnigan https://finnigan.dev/
// @description 05/01/2024, 17:09:39
// ==/UserScript==

// Iterate table and add a cell with a checkbox to each row
function addCheckboxes() {
  ds = JSON.parse(localStorage.getItem(localName))
  rows = document.querySelector("table.ta").rows

  // Iterate table
  for( i in rows ) {
    if(Object.hasOwn(rows, i)) {

      // Handle all other rows
      row = rows[i]
      cells = row.cells

      // Handle header
      if(i == 0) {
        cell = document.createElement("td")
        f = document.createTextNode("Got")
        cell.appendChild(f)
        row.appendChild(cell)
        continue
      }

      // Extend rows that have a colspan to new length
      if( cells.length === 1 ) {
        cell = cells[0]
        if( cell.hasAttribute("colspan") ) {
          cell.setAttribute("colspan", 7)
        }
        continue
      }

      // Handle body rows
      if(cells.length >= 5) {
        checkbox = document.createElement("input")
        checkbox.setAttribute("type", "checkbox")
        checkbox.id = 'found' + i
        checkbox.index = i
        checkbox.value = ds[i]
        checkbox.checked = ds[i]
        checkbox.onclick = function() { storeBrickGot(this.checked, this.index); };

        cell = document.createElement("td")
        cell.appendChild(checkbox)
        cell.setAttribute("align", "CENTER")
        row.appendChild(cell)
      }
    }
  }
}

// Get the Set ID from the URL params
function getSetID() {
  const queryString = window.location.search;
  const urlParams = new URLSearchParams(queryString);
  return urlParams.get('S')
}

// Create the datastructure backing the UI
function createDS(localName) {
  ds = JSON.parse(localStorage.getItem(localName))
  if(ds === null) {
    rows = document.querySelector("table.ta").rows.length
    ds = new Array(rows).fill(false);
    localStorage.setItem(localName, JSON.stringify(ds))

  }
}

// Update the backing datastructure
function storeBrickGot(checked, index) {
  ds = JSON.parse(localStorage.getItem(localName))
  ds[index] = checked
  localStorage.setItem(localName, JSON.stringify(ds))
}

// Global Constants
const localName = `userscript.gotbricks.${getSetID()}`;

(function() {
  // Main
  console.log("Got bricks?")
  createDS(localName);
  addCheckboxes();
})();