<commit_before>let detectByCursorStyle = false

export function setHintFindSettings (settings) {
  detectByCursorStyle = settings.hintDetectByCursorStyle
}

/** @type {WeakMap<HTMLElement, CSSStyleDeclaration>} */
let computedStyles

function demandComputedStyle (element) {
  if (computedStyles.has(element)) {
    return computedStyles.get(element)
  } else {
    const computedStyle = getComputedStyle(element)
    computedStyles.set(element, computedStyle)
    return computedStyle
  }
}

/**
 * Finds hints
 * @param {string} hintType - the type of elements to find (currently unused)
 */
export function findHints (hintType) {
  // on Firefox, getComputedStyle() may return null for conditions I don't fully understand
  // try-catch block prevents link hints generation from breaking.
  // https://bugzilla.mozilla.org/show_bug.cgi?id=548397
  try {
    // 1. getComputedStyle for every element
    const allElements = document.querySelectorAll('*')
    computedStyles = new WeakMap()
    // allElements.forEach((element) => computedStyles.set(element, getComputedStyle(element)));
    // 2. find hintable elements
    const hintableElements = []
    allElements.forEach(element => {
      if (isClickable(element)) {
        const rect = firstVisibleRect(element)
        if (rect) {
          const computedStyle = demandComputedStyle(element)
          hintableElements.push({
            element,
            rect: removeRectPaddingAndBorders(element, rect, computedStyle),
            computedStyle
          })
        }
      }
    })
    computedStyles = undefined
    console.log(hintableElements)
    return hintableElements
  } catch (e) {
    return []
  }
}

// based on https://github.com/guyht/vimari/blob/master/vimari.safariextension/linkHints.js
function isClickable (element, computedStyle) {
  // clickable html elements
  switch (element.nodeName) {
    case 'A':
    case 'BUTTON':
    case 'SELECT':
    case 'TEXTAREA':
      return true
    case 'INPUT':
      return element.type !== 'hidden'
  }
  // ARIA roles implying clickability
  switch (element.getAttribute('role')) {
    case 'button':
    case 'checkbox':
    case 'combobox':
    case 'link':
    case 'menuitem':
    case 'menuitemcheckbox':
    case 'menuitemradio':
    case 'radio':
    case 'tab':
    case 'textbox':<commit_msg>suppress console output of hintable elements in release build<commit_after>let detectByCursorStyle = false

export function setHintFindSettings (settings) {
  detectByCursorStyle = settings.hintDetectByCursorStyle
}

/** @type {WeakMap<HTMLElement, CSSStyleDeclaration>} */
let computedStyles

function demandComputedStyle (element) {
  if (computedStyles.has(element)) {
    return computedStyles.get(element)
  } else {
    const computedStyle = getComputedStyle(element)
    computedStyles.set(element, computedStyle)
    return computedStyle
  }
}

/**
 * Finds hints
 * @param {string} hintType - the type of elements to find (currently unused)
 */
export function findHints (hintType) {
  // on Firefox, getComputedStyle() may return null for conditions I don't fully understand
  // try-catch block prevents link hints generation from breaking.
  // https://bugzilla.mozilla.org/show_bug.cgi?id=548397
  try {
    // 1. getComputedStyle for every element
    const allElements = document.querySelectorAll('*')
    computedStyles = new WeakMap()
    // allElements.forEach((element) => computedStyles.set(element, getComputedStyle(element)));
    // 2. find hintable elements
    const hintableElements = []
    allElements.forEach(element => {
      if (isClickable(element)) {
        const rect = firstVisibleRect(element)
        if (rect) {
          const computedStyle = demandComputedStyle(element)
          hintableElements.push({
            element,
            rect: removeRectPaddingAndBorders(element, rect, computedStyle),
            computedStyle
          })
        }
      }
    })
    computedStyles = undefined
    if (SAKA_DEBUG) console.log(hintableElements)
    return hintableElements
  } catch (e) {
    return []
  }
}

// based on https://github.com/guyht/vimari/blob/master/vimari.safariextension/linkHints.js
function isClickable (element, computedStyle) {
  // clickable html elements
  switch (element.nodeName) {
    case 'A':
    case 'BUTTON':
    case 'SELECT':
    case 'TEXTAREA':
      return true
    case 'INPUT':
      return element.type !== 'hidden'
  }
  // ARIA roles implying clickability
  switch (element.getAttribute('role')) {
    case 'button':
    case 'checkbox':
    case 'combobox':
    case 'link':
    case 'menuitem':
    case 'menuitemcheckbox':
    case 'menuitemradio':
    case 'radio':
    case 'tab':
    case 'textbox':
{'fraction_non_alphanumeric': 0.10631088521812791, 'fraction_numerical': 0.0033883947479881405, 'mean_word_length': 3.220732797140304, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2474015', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1363, 'n_words': 343}