List Page Shield scripts

Lists all scripts detected by Page Shield.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * List Page Shield scripts
8
 * Lists all scripts detected by Page Shield.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_id: string,
13
  exclude_urls: string | undefined,
14
  urls: string | undefined,
15
  hosts: string | undefined,
16
  page: string | undefined,
17
  per_page: string | undefined,
18
  order_by: "first_seen_at" | "last_seen_at" | undefined,
19
  direction: "asc" | "desc" | undefined,
20
  prioritize_malicious: string | undefined,
21
  exclude_cdn_cgi: string | undefined,
22
  exclude_duplicates: string | undefined,
23
  status: string | undefined,
24
  page_url: string | undefined,
25
  _export: "csv" | undefined
26
) {
27
  const url = new URL(
28
    `https://api.cloudflare.com/client/v4/zones/${zone_id}/page_shield/scripts`
29
  );
30
  for (const [k, v] of [
31
    ["exclude_urls", exclude_urls],
32
    ["urls", urls],
33
    ["hosts", hosts],
34
    ["page", page],
35
    ["per_page", per_page],
36
    ["order_by", order_by],
37
    ["direction", direction],
38
    ["prioritize_malicious", prioritize_malicious],
39
    ["exclude_cdn_cgi", exclude_cdn_cgi],
40
    ["exclude_duplicates", exclude_duplicates],
41
    ["status", status],
42
    ["page_url", page_url],
43
    ["export", _export],
44
  ]) {
45
    if (v !== undefined && v !== "") {
46
      url.searchParams.append(k, v);
47
    }
48
  }
49
  const response = await fetch(url, {
50
    method: "GET",
51
    headers: {
52
      "X-AUTH-EMAIL": auth.email,
53
      "X-AUTH-KEY": auth.key,
54
      Authorization: "Bearer " + auth.token,
55
    },
56
    body: undefined,
57
  });
58
  if (!response.ok) {
59
    const text = await response.text();
60
    throw new Error(`${response.status} ${text}`);
61
  }
62
  return await response.json();
63
}
64