| const getElements = () => { |
| let toList = []; |
| const ref = window; |
| const parentWidth = ref.offsetWidth |
| const parentHeight = ref.offsetHeight |
| const parentScrollHeight = ref.scrollHeight |
| let elements = []; |
| elements = document.querySelectorAll('body *') |
| toList.splice(0, toList.length); |
| for (let el of elements) { |
| if (['script'].includes(el.tagName.toLowerCase())) continue |
| if (isUnvisible(el)) continue |
| toList.push(el) |
| } |
| toList.forEach((el) => { |
| let clientRect = el.getBoundingClientRect() |
| let pos = { |
| left: clientRect.left + scrollX, |
| right: clientRect.right + scrollX, |
| top: clientRect.top + scrollY, |
| bottom: clientRect.bottom + scrollY, |
| width: clientRect.width, |
| height: clientRect.height |
| } |
| const isFixedOrSticky = ['fixed', 'sticky'].includes( |
| getComputedStyle(el).position |
| ) |
| if (isFixedOrSticky) { |
| pos.left = clientRect.left |
| pos.right = clientRect.right |
| pos.top = clientRect.top |
| pos.bottom = clientRect.bottom |
| } |
| let biasX = [] |
| let biasY = [] |
| if (pos.left < parentWidth / 2) biasX = ['left'] |
| else biasX = ['right'] |
| if (pos.left < parentWidth / 2 && pos.right > parentWidth / 2) biasX.push('mid') |
|
|
| if (pos.top < parentHeight / 2) biasY = ['top'] |
| else if (pos.top > parentScrollHeight - parentHeight / 2) biasY = ['bottom'] |
| else biasY = ['scroll'] |
| if (pos.top < parentHeight / 2 && pos.bottom > parentHeight / 2 && isFixedOrSticky) |
| biasY.push('mid') |
|
|
| el.alisaInfo = { |
| elInfo: { |
| pos: pos, |
| biasX, |
| biasY |
| }, |
| parentInfo: { |
| width: parentWidth, |
| height: parentHeight, |
| scrollHeight: parentScrollHeight |
| }, |
| groupLeft: [], |
| groupRight: [], |
| groupTop: [], |
| groupBottom: [], |
| groupAxisWidth: [], |
| groupAxisHeight: [], |
| groupAll: [], |
| raceList: [], |
| assElement: null, |
| assLayoutSimScore: 0, |
| assStyleSimScore: 0 |
| } |
| }) |
|
|
| toList.forEach((el) => { |
| getSameGroup(el, toList, 'left') |
| getSameGroup(el, toList, 'right') |
| getSameGroup(el, toList, 'top') |
| getSameGroup(el, toList, 'bottom') |
| getSameGroup(el, toList, 'axisWidth') |
| getSameGroup(el, toList, 'axisHeight') |
| }) |
| toList.forEach((el) => { |
| getSameRace(el, toList) |
| }) |
|
|
| return toList; |
| } |
|
|
| const isUnvisible = (el) => { |
| const style = getComputedStyle(el); |
| const rect = el.getBoundingClientRect(); |
| const hidden = |
| style.display === 'none' || |
| style.visibility === 'hidden' || |
| style.opacity === '0' || rect.width === 0 || |
| rect.height === 0; |
| return hidden; |
| } |
|
|
| const getSameGroup = (el, elList, direction, offset = 5) => { |
| let idx = elList.indexOf(el) |
| if (idx < 0) return |
| const Direction = direction.slice(0, 1).toUpperCase() + direction.slice(1) |
| const isSamePos = (direction, pos1, pos2, offset) => { |
| if (['left', 'top', 'right', 'bottom'].includes(direction)) |
| if (Math.abs(pos1[direction] - pos2[direction]) <= offset) return true |
| if (direction == 'axisWidth') |
| if (Math.abs(pos1.width / 2 + pos1.left - pos2.width / 2 - pos2.left) <= offset) |
| return true |
| if (direction == 'axisHeight') |
| if (Math.abs(pos1.height / 2 + pos1.top - pos2.height / 2 - pos2.top) <= offset) |
| return true |
| return false |
| } |
| elList.forEach((element, i) => { |
| if (idx == i) return |
| if ( |
| isSamePos( |
| direction, |
| el.alisaInfo.elInfo.pos, |
| element.alisaInfo.elInfo.pos, |
| offset |
| ) |
| ) { |
| el.alisaInfo[`group${Direction}`].push(element) |
| let itemIndex = el.alisaInfo.groupAll.indexOf(element) |
| if (itemIndex < 0) el.alisaInfo.groupAll.push(element) |
| } |
| }) |
| } |
|
|
| const getSameRace = (el, elList) => { |
| let idx = elList.indexOf(el) |
| if (idx < 0) return |
| const isSameRace = (el1, el2) => { |
| const groupAll = el1.alisaInfo.groupAll |
| if (groupAll.indexOf(el2) < 0) return false |
| if (el1.tagName != el2.tagName) return false |
| return el1.className == el2.className |
| } |
| elList.forEach((element, i) => { |
| if (idx == i) return |
| if (isSameRace(el, element)) { |
| el.alisaInfo.raceList.push(element) |
| } |
| }) |
| } |
|
|
| const computedRawElementRatio = (offset = 5) => { |
| const toList = getElements(); |
| try { |
| let docMargin = parseFloat( |
| getComputedStyle(document.body).marginLeft |
| ) |
| let count = 0 |
| toList.forEach((el) => { |
| if (Math.abs(docMargin - el.alisaInfo.elInfo.pos.left) < offset) { |
| if (getComputedStyle(el).position === 'static') { |
| count++ |
| } |
| } |
| }) |
| console.log(count / toList.length) |
| return count / toList.length |
| } catch (e) { |
| console.error('Error in getting iframe document margin', e) |
| return -1 |
| } |
| } |
|
|
| const computedElementNum = () => { |
| const toList = getElements(); |
| let allTargetEls = toList |
| let viewedEls = [] |
| let count = 0 |
| allTargetEls.forEach((ei) => { |
| if (viewedEls.indexOf(ei) < 0) { |
| count += 1 |
| viewedEls.push(ei) |
| ei.alisaInfo.raceList.forEach((ri) => { |
| viewedEls.push(ri) |
| }) |
| } |
| }) |
| return count |
| } |