Skip to content

Commit 40f4202

Browse files
authored
Merge pull request #3 from nucliweb/first-third-party-scripts
Thank you @jhadev great job 👏🏼
2 parents e3638d7 + 4211da6 commit 40f4202

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

README.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,72 @@ function getImgs(sortBy) {
175175
}
176176
console.table(getImgs('encodedBodySize'))
177177

178-
```
178+
```
179+
180+
### First And Third Party Script Info
181+
182+
List all scripts using PerformanceResourceTiming API and separating them by first and third party
183+
184+
[More Info](https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming)
185+
186+
[Info On CORS](https://developer.mozilla.org/en-US/docs/Web/API/Resource_Timing_API/Using_the_Resource_Timing_API#coping_with_cors)
187+
188+
```js
189+
// ex: katespade.com - list firsty party subdomains in HOSTS array
190+
const HOSTS = ["assets.katespade.com"];
191+
192+
function getScriptInfo() {
193+
const resourceListEntries = performance.getEntriesByType("resource");
194+
// set for first party scripts
195+
const first = [];
196+
// set for third party scripts
197+
const third = [];
198+
199+
resourceListEntries.forEach((resource) => {
200+
// check for initiator type
201+
const value = "initiatorType" in resource;
202+
if (value) {
203+
if (resource.initiatorType === "script") {
204+
const { host } = new URL(resource.name);
205+
// check if resource url host matches location.host = first party script
206+
if (host === location.host || HOSTS.includes(host)) {
207+
const json = resource.toJSON();
208+
first.push({ ...json, type: "First Party" });
209+
} else {
210+
// add to third party script
211+
const json = resource.toJSON();
212+
third.push({ ...json, type: "Third Party" });
213+
}
214+
}
215+
}
216+
});
217+
218+
return {
219+
...(first.length && { firstParty: first }),
220+
...(third.length && { thirdParty: third }),
221+
};
222+
}
223+
224+
const { firstParty, thirdParty } = getScriptInfo();
225+
226+
console.groupCollapsed("FIRST PARTY SCRIPTS");
227+
console.table(firstParty);
228+
console.groupEnd();
229+
console.groupCollapsed("THIRD PARTY SCRIPTS");
230+
console.table(thirdParty);
231+
console.groupEnd();
232+
233+
/*
234+
Choose which properties to display
235+
https://developer.mozilla.org/en-US/docs/Web/API/console/table
236+
237+
console.groupCollapsed("FIRST PARTY SCRIPTS");
238+
console.table(firstParty, ["name", "nextHopProtocol"]);
239+
console.groupEnd();
240+
console.groupCollapsed("THIRD PARTY SCRIPTS", ["name", "nextHopProtocol"]);
241+
console.group();
242+
console.table(thirdParty);
243+
console.groupEnd();
244+
*/
245+
246+
```

0 commit comments

Comments
 (0)