Skip to content

Commit

Permalink
Keep moving related scriptlets into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Nov 8, 2024
1 parent ce4908b commit e5a0887
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 340 deletions.
17 changes: 9 additions & 8 deletions assets/resources/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
web page context.
*/

import { registerScriptlet } from './base.js';
import { runAt } from './run-at.js';
import { safeSelf } from './safe-self.js';

Expand Down Expand Up @@ -95,13 +96,13 @@ export function setAttrFn(
};
runAt(( ) => { start(); }, 'idle');
}
setAttrFn.details = {
registerScriptlet(setAttrFn, {
name: 'set-attr.fn',
dependencies: [
runAt,
safeSelf,
],
};
});

/**
* @scriptlet set-attr
Expand Down Expand Up @@ -149,14 +150,14 @@ export function setAttr(

setAttrFn(logPrefix, selector, attr, value);
}
setAttr.details = {
registerScriptlet(setAttr, {
name: 'set-attr.js',
dependencies: [
safeSelf,
setAttrFn,
],
world: 'ISOLATED',
};
});

/**
* @trustedScriptlet trusted-set-attr
Expand Down Expand Up @@ -188,15 +189,15 @@ export function trustedSetAttr(
const logPrefix = safe.makeLogPrefix('trusted-set-attr', selector, attr, value);
setAttrFn(logPrefix, selector, attr, value);
}
trustedSetAttr.details = {
registerScriptlet(trustedSetAttr, {
name: 'trusted-set-attr.js',
requiresTrust: true,
dependencies: [
safeSelf,
setAttrFn,
],
world: 'ISOLATED',
};
});

/**
* @scriptlet remove-attr
Expand Down Expand Up @@ -291,7 +292,7 @@ export function removeAttr(
};
runAt(( ) => { start(); }, behavior.split(/\s+/));
}
removeAttr.details = {
registerScriptlet(removeAttr, {
name: 'remove-attr.js',
aliases: [
'ra.js',
Expand All @@ -300,6 +301,6 @@ removeAttr.details = {
runAt,
safeSelf,
],
};
});

/******************************************************************************/
40 changes: 40 additions & 0 deletions assets/resources/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
uBlock Origin - a comprehensive, efficient content blocker
Copyright (C) 2019-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
The scriptlets below are meant to be injected only into a
web page context.
*/

export const registeredScriptlets = [];

export const registerScriptlet = (fn, details) => {
if ( typeof details !== 'object' ) {
throw new ReferenceError('Missing scriptlet details');
}
details.fn = fn;
fn.details = details;
if ( Array.isArray(details.dependencies) ) {
details.dependencies.forEach((fn, i, array) => {
if ( typeof fn !== 'function' ) { return; }
array[i] = fn.details.name;
});
}
registeredScriptlets.push(details);
};
55 changes: 28 additions & 27 deletions assets/resources/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
web page context.
*/

import { registerScriptlet } from './base.js';
import { safeSelf } from './safe-self.js';

/******************************************************************************/
Expand All @@ -47,9 +48,9 @@ export function getSafeCookieValuesFn() {
'yes', 'y', 'no', 'n',
];
}
getSafeCookieValuesFn.details = {
registerScriptlet(getSafeCookieValuesFn, {
name: 'get-safe-cookie-values.fn',
};
});

/******************************************************************************/

Expand All @@ -63,9 +64,9 @@ export function getAllCookiesFn() {
return { key, value };
}).filter(s => s !== undefined);
}
getAllCookiesFn.details = {
registerScriptlet(getAllCookiesFn, {
name: 'get-all-cookies.fn',
};
});

/******************************************************************************/

Expand All @@ -79,9 +80,9 @@ export function getCookieFn(
return s.slice(pos+1).trim();
}
}
getCookieFn.details = {
registerScriptlet(getCookieFn, {
name: 'get-cookie.fn',
};
});

/******************************************************************************/

Expand Down Expand Up @@ -142,12 +143,12 @@ export function setCookieFn(

return done;
}
setCookieFn.details = {
registerScriptlet(setCookieFn, {
name: 'set-cookie.fn',
dependencies: [
'get-cookie.fn',
getCookieFn,
],
};
});

/**
* @scriptlet set-cookie
Expand Down Expand Up @@ -200,27 +201,27 @@ export function setCookie(
safe.uboLog(logPrefix, 'Done');
}
}
setCookie.details = {
registerScriptlet(setCookie, {
name: 'set-cookie.js',
world: 'ISOLATED',
dependencies: [
'get-safe-cookie-values.fn',
'safe-self.fn',
'set-cookie.fn',
getSafeCookieValuesFn,
safeSelf,
setCookieFn,
],
};
});

// For compatibility with AdGuard
export function setCookieReload(name, value, path, ...args) {
setCookie(name, value, path, 'reload', '1', ...args);
}
setCookieReload.details = {
registerScriptlet(setCookieReload, {
name: 'set-cookie-reload.js',
world: 'ISOLATED',
dependencies: [
'set-cookie.js',
setCookie,
],
};
});

/**
* @trustedScriptlet trusted-set-cookie
Expand Down Expand Up @@ -294,28 +295,28 @@ export function trustedSetCookie(
safe.uboLog(logPrefix, 'Done');
}
}
trustedSetCookie.details = {
registerScriptlet(trustedSetCookie, {
name: 'trusted-set-cookie.js',
requiresTrust: true,
world: 'ISOLATED',
dependencies: [
'safe-self.fn',
'set-cookie.fn',
safeSelf,
setCookieFn,
],
};
});

// For compatibility with AdGuard
export function trustedSetCookieReload(name, value, offsetExpiresSec, path, ...args) {
trustedSetCookie(name, value, offsetExpiresSec, path, 'reload', '1', ...args);
}
trustedSetCookieReload.details = {
registerScriptlet(trustedSetCookieReload, {
name: 'trusted-set-cookie-reload.js',
requiresTrust: true,
world: 'ISOLATED',
dependencies: [
'trusted-set-cookie.js',
trustedSetCookie,
],
};
});

/**
* @scriptlet remove-cookie
Expand Down Expand Up @@ -396,15 +397,15 @@ export function removeCookie(
}, { passive: true });
}
}
removeCookie.details = {
registerScriptlet(removeCookie, {
name: 'remove-cookie.js',
aliases: [
'cookie-remover.js',
],
world: 'ISOLATED',
dependencies: [
'safe-self.fn',
safeSelf,
],
};
});

/******************************************************************************/
Loading

0 comments on commit e5a0887

Please sign in to comment.