A short JS script to measure global scope pollution in a web environment (Made for yw-cond v1.4b)
To use this code you must call the following function: getGlobalPollution in your console while your site is running.
It detects and reports global scope pollution i.e., properties on the window object that are not part of a clean browser environment. Useful for debugging my shitty code.
Function Signature:
getGlobalPollution(options)optionsObject (optional)
Here are all the options lol:
| Option | Type | Default | Description |
|---|---|---|---|
countFunctions |
boolean | true |
Whether to include standalone functions in the result. |
countObjects |
boolean | true |
Whether to include plain objects ({}) in the result. |
countClasses |
boolean | true |
Whether to include ES6 classes in the result. |
ignoreDevTools |
boolean | true |
Whether to ignore known DevTools-injected globals ($_, $0, dir, etc.). Set to false to include them. |
The function returns the following object:
{
count: Number, // total number of polluted globals detected
polluted: Array // array of objects describing each polluted global
}Each element in the polluted array has the following structure:
{
name: string, // global variable name
kind: string, // 'class', 'object', 'array', 'function', 'number', 'string', etc.
source?: string // optional; present if ignoreDevTools is false and the global is DevTools/extension injected
}const result = getGlobalPollution();
console.log(result.count); // e.g., 5
console.log(result.polluted); // [{ name: 'myLib', kind: 'object' }, ...]By default, DevTools helpers are ignored, and all functions, objects, and classes are counted.
const result = getGlobalPollution({ ignoreDevTools: false });
console.log(result.polluted);const result = getGlobalPollution({ countObjects: false, countClasses: false });
console.log(result.polluted);
// Only shows functions defined globally (excluding classes and objects)const result = getGlobalPollution({ countFunctions: false, countClasses: false });
console.log(result.polluted);
// Only global objects and arrays are reported- Creates a hidden iframe to capture a clean global environment.
- Compares all properties of the current
windowagainst the clean iframe. - Filters out:
- Native browser globals.
- Frame index aliases (
"0","1", …). - Known DevTools helpers (optional).
- Classifies remaining globals as
class,object,array,function, or primitive type. - Returns the data
Notes:
constandletdeclarations in scripts are not attached towindow, so they are not detected as globals by this function. Onlyvar, functions, or explicitly attached properties appear.- Some browser extensions may inject additional globals that will be reported if
ignoreDevToolsisfalse. - This function does not recursively inspect objects; it only checks top-level globals on
window.- I did this for my use case, but it'd be relatively simple to modify this code to do so. I'm lazy though and nobody'll use this so I won't.