-
Notifications
You must be signed in to change notification settings - Fork 2
/
getBlockingScriptandMethods.py
153 lines (137 loc) · 5.57 KB
/
getBlockingScriptandMethods.py
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
"""
This files contain the logic to gather tracking and mixed scripts or methods for blocking experiments.
"""
import math
import numpy as np
import pandas as pd
import json
import os
def getInitiatorScript(stack):
if len(stack["callFrames"]) != 0:
return stack["callFrames"][0]["url"]
else:
return getInitiatorScript(stack["parent"])
def getInitiatorMethod(stack):
if len(stack["callFrames"]) != 0:
return (
stack["callFrames"][0]["url"]
+ "@"
+ stack["callFrames"][0]["functionName"]
+ "@"
+ str(stack["callFrames"][0]["lineNumber"])
+ "@"
+ str(stack["callFrames"][0]["columnNumber"])
)
else:
return getInitiatorMethod(stack["parent"])
def addScript(script, key, tc, fc, topURL):
try:
if key not in script.keys():
script[key] = [0, 0, 0, []] # tc, fc, log10(tc/fc), toplevelurl
script[key][0] += tc
script[key][1] += fc
script[key][2] = math.log(
(script[key][0] + 0.0000001) / (script[key][1] + 0.0000001), 10
)
if topURL not in script[key][3]:
script[key][3].append(topURL)
except:
pass
def addMethod(method, key, tc, fc, topURL):
try:
if key not in method.keys():
method[key] = [0, 0, 0, [], []] # tc, fc, log10(tc/fc), toplevelurl
method[key][0] += tc
method[key][1] += fc
method[key][2] = math.log(
(method[key][0] + 0.0000001) / (method[key][1] + 0.0000001), 10
)
if topURL not in method[key][3]:
method[key][3].append(topURL)
except:
pass
def getScriptsMethods():
script = {}
method = {}
path = "/Users/haadi/Desktop/webpage-crawler-extension/server/output"
fold = os.listdir(path)
for f in fold:
if ".com" in f:
if os.path.isfile(path + "/" + f + "/label_request.json"):
print(f)
df = pd.read_json(path + "/" + f + "/label_request.json")
for index, dataset in df.iterrows():
try:
if dataset["call_stack"]["type"] == "script":
if (
dataset["easylistflag"] == 1
or dataset["easyprivacylistflag"] == 1
or dataset["ancestorflag"] == 1
):
addScript(
script,
getInitiatorScript(dataset["call_stack"]["stack"]),
1,
0,
dataset["top_level_url"],
)
addMethod(
method,
getInitiatorMethod(dataset["call_stack"]["stack"]),
1,
0,
dataset["top_level_url"],
)
else:
addScript(
script,
getInitiatorScript(dataset["call_stack"]["stack"]),
0,
1,
dataset["top_level_url"],
)
addMethod(
method,
getInitiatorMethod(dataset["call_stack"]["stack"]),
0,
1,
dataset["top_level_url"],
)
except:
pass
trackingScripts = []
mixedScripts = []
mixedScriptMethod = {}
for s in script.keys():
if script[s][2] >= -2 and script[s][2] <= 2:
if s != "" and "http" in s:
if s.find("http://") == 0 or s.find("https://") == 0:
mixedScripts.append(s)
elif script[s][2] > 2:
if s != "" and "http" in s:
if s.find("http://") == 0 or s.find("https://") == 0:
trackingScripts.append(s)
for m in method:
lst = m.split("@")
if lst[0] != "" and "http" in lst[0] and lst[0] in mixedScripts:
if lst[0].find("http://") == 0 or lst[0].find("https://") == 0:
if method[m][2] > 2:
if lst[0] not in mixedScriptMethod.keys():
mixedScriptMethod[lst[0]] = []
mixedScriptMethod[lst[0]].append([lst[1], lst[2], lst[3]])
elif method[m][2] >= -2 and method[m][2] <= 2:
if lst[0] not in mixedScriptMethod.keys():
mixedScriptMethod[lst[0]] = []
mixedScriptMethod[lst[0]].append([lst[1], lst[2], lst[3]])
# json.dump(script, open(path+'/'+ f + "/blockScripts.json", "w"))
# json.dump(method, open(path+'/'+ f + "/blockMethods.json", "w"))
with open(path + "/lst/trackingScripts.txt", "w") as log:
log.write(str(trackingScripts))
log.close()
with open(path + "/lst/mixedScripts.txt", "w") as log:
log.write(str(mixedScripts + trackingScripts))
log.close()
with open(path + "/lst/mixedScriptMethods.txt", "w") as log:
log.write(str(mixedScriptMethod))
log.close()
getScriptsMethods()