/
debugging.js
94 lines (85 loc) · 2.77 KB
/
debugging.js
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
import {config} from './config.js';
import {getHook, hook} from './hook.js';
import {getGlobal} from './prebidGlobal.js';
import {logMessage, prefixLog} from './utils.js';
import {createBid} from './bidfactory.js';
import {loadExternalScript} from './adloader.js';
import {GreedyPromise} from './utils/promise.js';
export const DEBUG_KEY = '__$$PREBID_GLOBAL$$_debugging__';
function isDebuggingInstalled() {
return getGlobal().installedModules.includes('debugging');
}
function loadScript(url) {
return new GreedyPromise((resolve) => {
loadExternalScript(url, 'debugging', resolve);
});
}
export function debuggingModuleLoader({alreadyInstalled = isDebuggingInstalled, script = loadScript} = {}) {
let loading = null;
return function () {
if (loading == null) {
loading = new GreedyPromise((resolve, reject) => {
// run this in a 0-delay timeout to give installedModules time to be populated
setTimeout(() => {
if (alreadyInstalled()) {
resolve();
} else {
const url = '$$PREBID_DIST_URL_BASE$$debugging-standalone.js';
logMessage(`Debugging module not installed, loading it from "${url}"...`);
getGlobal()._installDebugging = true;
script(url).then(() => {
getGlobal()._installDebugging({DEBUG_KEY, hook, config, createBid, logger: prefixLog('DEBUG:')});
}).then(resolve, reject);
}
});
})
}
return loading;
}
}
export function debuggingControls({load = debuggingModuleLoader(), hook = getHook('requestBids')} = {}) {
let promise = null;
let enabled = false;
function waitForDebugging(next, ...args) {
return (promise || GreedyPromise.resolve()).then(() => next.apply(this, args))
}
function enable() {
if (!enabled) {
promise = load();
// set debugging to high priority so that it has the opportunity to mess with most things
hook.before(waitForDebugging, 99);
enabled = true;
}
}
function disable() {
hook.getHooks({hook: waitForDebugging}).remove();
enabled = false;
}
function reset() {
promise = null;
disable();
}
return {enable, disable, reset};
}
const ctl = debuggingControls();
export const reset = ctl.reset;
export function loadSession() {
let storage = null;
try {
storage = window.sessionStorage;
} catch (e) {}
if (storage !== null) {
let debugging = ctl;
let config = null;
try {
config = storage.getItem(DEBUG_KEY);
} catch (e) {}
if (config !== null) {
// just make sure the module runs; it will take care of parsing the config (and disabling itself if necessary)
debugging.enable();
}
}
}
config.getConfig('debugging', function ({debugging}) {
debugging?.enabled ? ctl.enable() : ctl.disable();
});