File size: 5,834 Bytes
4a718ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
}