Skip to content

Commit

Permalink
Merge pull request #147 from yakisova41/trusted-inject
Browse files Browse the repository at this point in the history
✨ Feature trusted inject mode
  • Loading branch information
yakisova41 authored Aug 4, 2024
2 parents 57a4cb0 + a4e3fb4 commit 42b3c23
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions dev/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const manifest = {
connection_isolated: true,
userscript_direct_inject: true,
bind_GM_api: true,
trusted_inject: true,
},
{
matches: ['https://www.google.com/*'],
Expand Down
17 changes: 14 additions & 3 deletions packages/crx-monkey/src/node/handlers/UserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class UsersScript {
directInject: boolean = false,
bindGM: boolean = false,
defineGMHash: string | false = false,
trusted_inject: boolean = false,
) {
const syntaxs = {
document_end: {
Expand Down Expand Up @@ -143,9 +144,19 @@ export class UsersScript {
}

functionNames.forEach((name) => {
directScriptContent.push(
`script.innerHTML = script.innerHTML + \`(\${${name}.toString()})();\``,
);
if (trusted_inject) {
directScriptContent.push(
'const policy = window.trustedTypes.createPolicy("crx-monkey-trusted-inject-policy", {createScript: (input) => input,});',
);

directScriptContent.push(
`script.text = policy.createScript(script.text + \`(\${${name}.toString()})();\`)`,
);
} else {
directScriptContent.push(
`script.innerHTML = script.innerHTML + \`(\${${name}.toString()})();\``,
);
}
});

directScriptContent.push('unsafeWindow.document.body.appendChild(script)\n');
Expand Down
14 changes: 12 additions & 2 deletions packages/crx-monkey/src/node/handlers/build/BuildUserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,16 @@ export class BuildUserScript extends Build implements BuildImplements {

if (contentScripts !== undefined) {
contentScripts.forEach((contentScript) => {
const { matches, js, css, run_at, exclude_matches, userscript_direct_inject, bind_GM_api } =
contentScript;
const {
matches,
js,
css,
run_at,
exclude_matches,
userscript_direct_inject,
bind_GM_api,
trusted_inject,
} = contentScript;

let thisContentScriptCode = '';

Expand All @@ -279,6 +287,8 @@ export class BuildUserScript extends Build implements BuildImplements {
css,
userscript_direct_inject,
bind_GM_api,
false,
trusted_inject,
);

// End if.
Expand Down
3 changes: 2 additions & 1 deletion packages/crx-monkey/src/node/handlers/dev/WatchUserScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class WatchUserScript extends Watch implements WatchImplements {

if (contentScripts !== undefined) {
contentScripts.forEach((contentScript) => {
const { matches, js, css, exclude_matches, bind_GM_api } = contentScript;
const { matches, js, css, exclude_matches, bind_GM_api, trusted_inject } = contentScript;

if (matches !== undefined && !matches.includes('<all_urls>')) {
// Start conditional statement of if for branch of href.
Expand All @@ -294,6 +294,7 @@ export class WatchUserScript extends Watch implements WatchImplements {
false,
false,
bind_GM_api === true ? this.bindGMHash : false,
trusted_inject,
);

// End if.
Expand Down
1 change: 1 addition & 0 deletions packages/crx-monkey/src/node/manifest-factory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class ManifestFactory {
'userscript_direct_inject',
'connection_isolated',
'bind_GM_api',
'trusted_inject',
];

constructor(originalManifest: CrxMonkeyManifest) {
Expand Down
1 change: 1 addition & 0 deletions packages/crx-monkey/src/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ export type CrxMonkeyContentScripts = Array<{
userscript_direct_inject?: boolean;
connection_isolated?: boolean;
bind_GM_api?: boolean;
trusted_inject?: boolean;
[key: string]: unknown;
}>;

0 comments on commit 42b3c23

Please sign in to comment.