|
| 1 | +import ContentFeature from '../content-feature.js'; |
| 2 | +import { isBeingFramed } from '../utils.js'; |
| 3 | +import { setCookie } from './Scriptlets/src/scriptlets/set-cookie.js'; |
| 4 | +import { trustedSetCookie } from './Scriptlets/src/scriptlets/trusted-set-cookie.js'; |
| 5 | +import { setCookieReload } from './Scriptlets/src/scriptlets/set-cookie-reload.js'; |
| 6 | +import { removeCookie } from './Scriptlets/src/scriptlets/remove-cookie.js'; |
| 7 | +import { setConstant } from './Scriptlets/src/scriptlets/set-constant.js'; |
| 8 | +import { setLocalStorageItem } from './Scriptlets/src/scriptlets/set-local-storage-item.js'; |
| 9 | +import { abortCurrentInlineScript } from './Scriptlets/src/scriptlets/abort-current-inline-script.js'; |
| 10 | +import { abortOnPropertyRead } from './Scriptlets/src/scriptlets/abort-on-property-read.js'; |
| 11 | +import { abortOnPropertyWrite } from './Scriptlets/src/scriptlets/abort-on-property-write.js'; |
| 12 | +import { preventAddEventListener } from './Scriptlets/src/scriptlets/prevent-addEventListener.js'; |
| 13 | +import { preventWindowOpen } from './Scriptlets/src/scriptlets/prevent-window-open.js'; |
| 14 | +import { preventSetTimeout } from './Scriptlets/src/scriptlets/prevent-setTimeout.js'; |
| 15 | +import { removeNodeText } from './Scriptlets/src/scriptlets/remove-node-text.js'; |
| 16 | +import { preventFetch } from './Scriptlets/src/scriptlets/prevent-fetch.js'; |
| 17 | + |
| 18 | +export class Scriptlets extends ContentFeature { |
| 19 | + init() { |
| 20 | + if (isBeingFramed()) { |
| 21 | + return; |
| 22 | + } |
| 23 | + /* @type {import('./Scriptlets/src/scriptlets/scriptlets.ts').Source} */ |
| 24 | + const source = { |
| 25 | + verbose: false, |
| 26 | + }; |
| 27 | + |
| 28 | + const scriptlets = this.getFeatureSetting('scriptlets'); |
| 29 | + for (const scriptlet of scriptlets) { |
| 30 | + source.name = scriptlet.name; |
| 31 | + source.args = Object.values(scriptlet.attrs); |
| 32 | + this.runScriptlet(scriptlet, source); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + runScriptlet(scriptlet, source) { |
| 37 | + const attrs = scriptlet.attrs || {}; |
| 38 | + |
| 39 | + // add debug flag to site breakage reports |
| 40 | + this.addDebugFlag(); |
| 41 | + |
| 42 | + if (scriptlet.name === 'setCookie') { |
| 43 | + setCookie(source, attrs.name, attrs.value, attrs.path, attrs.domain); |
| 44 | + } |
| 45 | + if (scriptlet.name === 'trustedSetCookie') { |
| 46 | + trustedSetCookie(source, attrs.name, attrs.value, attrs.path, attrs.domain); |
| 47 | + } |
| 48 | + if (scriptlet.name === 'setCookieReload') { |
| 49 | + setCookieReload(source, attrs.name, attrs.value, attrs.path, attrs.domain); |
| 50 | + } |
| 51 | + if (scriptlet.name === 'removeCookie') { |
| 52 | + removeCookie(source, attrs.match); |
| 53 | + } |
| 54 | + if (scriptlet.name === 'setConstant') { |
| 55 | + setConstant(source, attrs.property, attrs.value, attrs.stack, attrs.valueWrapper, attrs.setProxyTrap); |
| 56 | + } |
| 57 | + if (scriptlet.name === 'setLocalStorageItem') { |
| 58 | + setLocalStorageItem(source, attrs.key, attrs.value); |
| 59 | + } |
| 60 | + if (scriptlet.name === 'abortCurrentInlineScript') { |
| 61 | + abortCurrentInlineScript(source, attrs.property, attrs.search); |
| 62 | + } |
| 63 | + if (scriptlet.name === 'abortOnPropertyRead') { |
| 64 | + abortOnPropertyRead(source, attrs.property); |
| 65 | + } |
| 66 | + if (scriptlet.name === 'abortOnPropertyWrite') { |
| 67 | + abortOnPropertyWrite(source, attrs.property); |
| 68 | + } |
| 69 | + if (scriptlet.name === 'preventAddEventListener') { |
| 70 | + preventAddEventListener(source, attrs.typeSearch, attrs.listenerSearch, attrs.additionalArgName, attrs.additionalArgValue); |
| 71 | + } |
| 72 | + if (scriptlet.name === 'preventWindowOpen') { |
| 73 | + preventWindowOpen(source, attrs.match, attrs.delay, attrs.replacement); |
| 74 | + } |
| 75 | + if (scriptlet.name === 'preventSetTimeout') { |
| 76 | + preventSetTimeout(source, attrs.matchCallback, attrs.matchDelay); |
| 77 | + } |
| 78 | + if (scriptlet.name === 'removeNodeText') { |
| 79 | + removeNodeText(source, attrs.nodeName, attrs.textMatch, attrs.parentSelector); |
| 80 | + } |
| 81 | + if (scriptlet.name === 'preventFetch') { |
| 82 | + preventFetch(source, attrs.propsToMatch, attrs.responseBody, attrs.responseType); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +export default Scriptlets; |
0 commit comments