Skip to content

Commit

Permalink
Inline script support (#378)
Browse files Browse the repository at this point in the history
* added functionality for inline script
  • Loading branch information
dharmesh-hemaram authored Aug 25, 2024
1 parent b78de01 commit 9ed97c6
Show file tree
Hide file tree
Showing 26 changed files with 2,797 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mono-repo [deploy]
run-name: Deploy to ${{ inputs.environment }} by @${{ github.actor }}
run-name: Deploy to ${{ inputs.environment }} by @${{ github.actor }} on ${{ inputs.version || github.ref_name }}

on:
workflow_dispatch:
Expand Down
28 changes: 28 additions & 0 deletions .verdaccio/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# path to a directory with all packages
storage: ../tmp/local-registry/storage

# a list of other known repositories we can talk to
uplinks:
npmjs:
url: https://registry.npmjs.org/
maxage: 60m

packages:
'**':
# give all users (including non-authenticated users) full access
# because it is a local registry
access: $all
publish: $all
unpublish: $all

# if package is not available locally, proxy requests to npm registry
proxy: npmjs

# log settings
logs:
type: stdout
format: pretty
level: warn

publish:
allow_offline: true # set offline to true to allow publish offline
4 changes: 4 additions & 0 deletions apps/acf-extension/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
"entryName": "content_scripts",
"entryPath": "apps/acf-extension/src/content_scripts/index.ts"
},
{
"entryName": "content_scripts_main",
"entryPath": "apps/acf-extension/src/content_scripts_main/index.ts"
},
{
"entryName": "wizard",
"entryPath": "apps/acf-extension/src/wizard/index.ts"
Expand Down
2 changes: 2 additions & 0 deletions apps/acf-extension/src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-new */
import { RUNTIME_MESSAGE_ACF } from '@dhruv-techapps/acf-common';
import { MainWorldBackground, RUNTIME_MESSAGE_MAIN_WORLD_MESSAGING } from '@dhruv-techapps/acf-main-world';
import { Runtime } from '@dhruv-techapps/core-extension';
import { DiscordMessagingBackground, RUNTIME_MESSAGE_DISCORD_MESSAGING } from '@dhruv-techapps/discord-messaging';
import { DiscordOauth2Background, RUNTIME_MESSAGE_DISCORD_OAUTH } from '@dhruv-techapps/discord-oauth';
Expand Down Expand Up @@ -84,6 +85,7 @@ try {
[RUNTIME_MESSAGE_FIREBASE_OAUTH]: new FirebaseOauth2Background(auth, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_FIREBASE_FIRESTORE]: new FirebaseFirestoreBackground(auth, EDGE_OAUTH_CLIENT_ID, OPTIONS_PAGE_URL),
[RUNTIME_MESSAGE_FIREBASE_FUNCTIONS]: new FirebaseFunctionsBackground(auth, EDGE_OAUTH_CLIENT_ID),
[RUNTIME_MESSAGE_MAIN_WORLD_MESSAGING]: new MainWorldBackground(),
};
Runtime.onMessageExternal(onMessageListener);
Runtime.onMessage(onMessageListener);
Expand Down
4 changes: 2 additions & 2 deletions apps/acf-extension/src/content_scripts/action.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ACTION_STATUS, Action } from '@dhruv-techapps/acf-common';
import { Events } from '@dhruv-techapps/acf-events';
import { STATUS_BAR_TYPE } from '@dhruv-techapps/status-bar';
import Common from './common';
import { statusBar } from './status-bar';
import { ACFEvents } from './util/acf-events';
import { ACFValue } from './util/acf-value';

const ActionProcessor = (() => {
Expand Down Expand Up @@ -32,7 +32,7 @@ const ActionProcessor = (() => {
return ACTION_STATUS.SKIPPED;
}
const value = action.value ? await ACFValue.getValue(action.value) : action.value;
await Events.check(elements, value);
await ACFEvents.check(elementFinder, elements, value);
};

const start = async (action: Action) => {
Expand Down
2 changes: 1 addition & 1 deletion apps/acf-extension/src/content_scripts/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ActionSettings, RETRY_OPTIONS } from '@dhruv-techapps/acf-common';
import { SettingsStorage } from '@dhruv-techapps/acf-store';
import { ConfigError, Logger } from '@dhruv-techapps/core-common';
import { statusBar } from './status-bar';
import { I18N_ERROR } from './i18n';
import { statusBar } from './status-bar';

const Common = (() => {
const retryFunc = async (retry?: number, retryInterval?: number | string) => {
Expand Down
13 changes: 13 additions & 0 deletions apps/acf-extension/src/content_scripts/util/acf-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Events } from '@dhruv-techapps/acf-events';
import { MainWorldService } from '@dhruv-techapps/acf-main-world';

export class ACFEvents {
static async check(elementFinder: string, elements: Array<HTMLElement>, value?: string) {
const element = elements[0];
if (element.tagName === 'A' && /javascript/gi.test((element as HTMLAnchorElement).href)) {
MainWorldService.click(elementFinder);
return;
}
await Events.check(elements, value);
}
}
44 changes: 44 additions & 0 deletions apps/acf-extension/src/content_scripts_main/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const Common = (() => {
const getElements = async (elementFinder: string): Promise<Array<HTMLElement> | undefined> => {
let elements: HTMLElement[] | undefined;
try {
if (/^(id::|#)/gi.test(elementFinder)) {
const element = document.getElementById(elementFinder.replace(/^(id::|#)/gi, ''));
elements = element ? [element] : undefined;
} else if (/^Selector::/gi.test(elementFinder)) {
const element = document.querySelector<HTMLElement>(elementFinder.replace(/^Selector::/gi, ''));
elements = element ? [element] : undefined;
} else if (/^ClassName::/gi.test(elementFinder)) {
const classElements = document.getElementsByClassName(elementFinder.replace(/^ClassName::/gi, '')) as HTMLCollectionOf<HTMLElement>;
elements = classElements.length !== 0 ? Array.from(classElements) : undefined;
} else if (/^Name::/gi.test(elementFinder)) {
const nameElements = document.getElementsByName(elementFinder.replace(/^Name::/gi, ''));
elements = nameElements.length !== 0 ? Array.from(nameElements) : undefined;
} else if (/^TagName::/gi.test(elementFinder)) {
const tagElements = document.getElementsByTagName(elementFinder.replace(/^TagName::/gi, '')) as HTMLCollectionOf<HTMLElement>;
elements = tagElements.length !== 0 ? Array.from(tagElements) : undefined;
} else if (/^SelectorAll::/gi.test(elementFinder)) {
const querySelectAll = document.querySelectorAll<HTMLElement>(elementFinder.replace(/^SelectorAll::/gi, ''));
elements = querySelectAll.length !== 0 ? Array.from(querySelectAll) : undefined;
} else {
const nodes = document.evaluate(elementFinder, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (nodes.snapshotLength !== 0) {
elements = [];
let i = 0;
while (i < nodes.snapshotLength) {
elements.push(nodes.snapshotItem(i) as HTMLElement);
i += 1;
}
}
}
} catch (e) {
console.error(e);
}

return elements;
};

return { getElements };
})();

export default Common;
10 changes: 10 additions & 0 deletions apps/acf-extension/src/content_scripts_main/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as ACFCommon from './common';

declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ACFCommon: any;
}
}

window.ACFCommon = ACFCommon;
11 changes: 9 additions & 2 deletions apps/acf-extension/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"version": "5.0.0",
"manifest_version": 3,
"default_locale": "en",
"permissions": ["storage", "notifications", "contextMenus", "activeTab", "identity", "alarms", "unlimitedStorage"],
"optional_host_permissions": ["http://*/*", "https://*/*"],
"permissions": ["storage", "notifications", "contextMenus", "activeTab", "identity", "alarms", "unlimitedStorage", "scripting"],
"host_permissions": ["http://*/*", "https://*/*"],
"icons": {
"16": "assets/icons/icon16.png",
"32": "assets/icons/icon32.png",
Expand All @@ -30,6 +30,13 @@
"run_at": "document_start",
"all_frames": true
},
{
"matches": ["<all_urls>"],
"js": ["content_scripts_main.js"],
"run_at": "document_start",
"all_frames": true,
"world": "MAIN"
},
{
"matches": ["<all_urls>"],
"css": ["css/wizard-popup.min.css"],
Expand Down
25 changes: 25 additions & 0 deletions libs/acf/main-world/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
11 changes: 11 additions & 0 deletions libs/acf/main-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# acf-main-world

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build acf-main-world` to build the library.

## Running unit tests

Run `nx test acf-main-world` to execute the unit tests via [Jest](https://jestjs.io).
10 changes: 10 additions & 0 deletions libs/acf/main-world/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'acf-main-world',
preset: '../../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../../coverage/libs/acf/main-world',
};
11 changes: 11 additions & 0 deletions libs/acf/main-world/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@dhruv-techapps/acf-main-world",
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0",
"@dhruv-techapps/core-service": "*"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
42 changes: 42 additions & 0 deletions libs/acf/main-world/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "acf-main-world",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/acf/main-world/src",
"projectType": "library",
"release": {
"version": {
"generatorOptions": {
"packageRoot": "dist/{projectRoot}",
"currentVersionResolver": "git-tag"
}
}
},
"tags": ["lib:main-world"],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/libs/acf/main-world",
"main": "libs/acf/main-world/src/index.ts",
"tsConfig": "libs/acf/main-world/tsconfig.lib.json",
"assets": ["libs/acf/main-world/*.md"]
}
},
"nx-release-publish": {
"options": {
"packageRoot": "dist/{projectRoot}"
}
},
"lint": {
"executor": "@nx/eslint:lint"
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/acf/main-world/jest.config.ts"
}
}
}
}
5 changes: 5 additions & 0 deletions libs/acf/main-world/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="chrome"/>

export * from './lib/main-world-background';
export * from './lib/main-world.constant';
export * from './lib/main-world.service';
26 changes: 26 additions & 0 deletions libs/acf/main-world/src/lib/main-world-background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
declare global {
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ACFCommon: any;
}
}

export class MainWorldBackground {
async click(message: string, sender: chrome.runtime.MessageSender) {
if (sender.tab?.id) {
// Perform the action that requires the permission
chrome.scripting.executeScript<[string], void>({
world: 'MAIN',
target: { tabId: sender.tab.id },
func: (message: string) => {
window.ACFCommon.default.getElements(message).then((elements: Array<HTMLElement>) => {
elements.forEach((element) => {
element.click();
});
});
},
args: [message],
});
}
}
}
1 change: 1 addition & 0 deletions libs/acf/main-world/src/lib/main-world.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const RUNTIME_MESSAGE_MAIN_WORLD_MESSAGING = 'main-world';
9 changes: 9 additions & 0 deletions libs/acf/main-world/src/lib/main-world.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CoreService } from '@dhruv-techapps/core-service';
import { RUNTIME_MESSAGE_MAIN_WORLD_MESSAGING } from './main-world.constant';

export class MainWorldService extends CoreService {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static async click(elementFinder: string) {
return await this.message({ messenger: RUNTIME_MESSAGE_MAIN_WORLD_MESSAGING, methodName: 'click', message: elementFinder });
}
}
22 changes: 22 additions & 0 deletions libs/acf/main-world/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions libs/acf/main-world/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
9 changes: 9 additions & 0 deletions libs/acf/main-world/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
}
3 changes: 2 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
"firebase-oauth",
"firebase-firestore",
"firebase-functions",
"firebase-database"
"firebase-database",
"acf-main-world"
],
"changelog": {
"workspaceChangelog": {
Expand Down
Loading

0 comments on commit 9ed97c6

Please sign in to comment.