Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions helpers/postprocess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import fs from "fs";
import { createRequire } from "module";

const require = createRequire(import.meta.url);
const manifest = require("../dist/manifest.json");
console.log(manifest.web_accessible_resources);

manifest.web_accessible_resources.forEach((resource, i) => {
manifest.web_accessible_resources[i].use_dynamic_url = false;
console.log(resource.matches[0])
if (resource.matches[0] == "<all_urls>") {
manifest.web_accessible_resources[i].matches[0] = "https://powerschool.sas.edu.sg/*";
}
});

console.log(manifest.web_accessible_resources);



if (fs.existsSync("./dist/manifest.json")) {
fs.writeFileSync("./dist/manifest.json", JSON.stringify(manifest, null, 2));
} else {
throw new Error("Manifest file not found");
}
128 changes: 128 additions & 0 deletions helpers/viterunplugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// From https://github.com/pnd280/complexity/blob/alpha/vite-plugins/vite-plugin-run-command-on-demand.ts

import chalk from "chalk";
import { exec } from "child_process";


const pluginName = "vite-plugin-run-command-on-demand";

/**
* Logs a message with the plugin name.
* @param {string} message - The message to log.
*/
const log = (message) => console.log(chalk.blue(`\n[${pluginName}]`), message);

/**
* Logs an error message with the plugin name.
* @param {string} message - The error message to log.
*/
const logError = (message) =>
console.error(chalk.blue(`\n[${pluginName}]`), chalk.red(message), "\n");

/**
* Runs a shell command.
* @param {string} command - The command to run.
* @returns {Promise<void>} A promise that resolves when the command completes.
*/
const runCommand = (command) =>
new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
logError(`Error executing command: ${command}\n${stderr}`);
reject(error);
} else {
log(`Command executed successfully: ${command}\n${stdout}`);
resolve();
}
});
});

/**
* @typedef {Object} CustomCommandsPluginOptions
* @property {string} [beforeServerStart] - Command to run before the server starts.
* @property {string} [afterServerStart] - Command to run after the server starts.
* @property {string} [onHotUpdate] - Command to run on hot update.
* @property {string} [beforeBuild] - Command to run before the build starts.
* @property {string} [afterBuild] - Command to run after the build ends.
* @property {string} [closeBundle] - Command to run when the bundle is closed.
*/

/**
* Executes a command if it is defined.
* @param {string|undefined} command - The command to execute.
* @param {string} errorMessage - The error message to log if the command fails.
*/
const executeCommand = async (command, errorMessage) => {
if (command) {
try {
await runCommand(command);
} catch {
logError(errorMessage);
}
}
};

/**
* Checks if the current environment is allowed.
* @returns {boolean} True if the environment is "development" or "production".
*/
const isAllowedEnvironment = () => {
const env = process.env.NODE_ENV;
return env === "development" || env === "production";
};

/**
* Vite plugin to run custom commands on demand.
* @param {CustomCommandsPluginOptions} [options={}] - The plugin options.

*/
export default function customCommandsPlugin(options = {}) {
return {
name: pluginName,
configureServer(server) {
if (!isAllowedEnvironment()) return;
server.httpServer?.once("listening", async () => {
await executeCommand(
options.beforeServerStart,
`Error running beforeServerStart command: ${options.beforeServerStart}`
);
await executeCommand(
options.afterServerStart,
`Error running afterServerStart command: ${options.afterServerStart}`
);
});
},
async handleHotUpdate(ctx) {
if (!isAllowedEnvironment()) return ctx.modules;
const isPageReload = ctx.modules.some((module) => !module.isSelfAccepting);
if (!isPageReload) {
await executeCommand(
options.onHotUpdate,
`Error running onHotUpdate command: ${options.onHotUpdate}`
);
}
return ctx.modules;
},
async buildStart() {
if (!isAllowedEnvironment()) return;
await executeCommand(
options.beforeBuild,
`Error running beforeBuild command: ${options.beforeBuild}`
);
},
async buildEnd() {
if (!isAllowedEnvironment()) return;
await executeCommand(
options.afterBuild,
`Error running afterBuild command: ${options.afterBuild}`
);
},
async closeBundle() {
if (!isAllowedEnvironment()) return;
await executeCommand(
options.closeBundle,
`Error running closeBundle command: ${options.closeBundle}`
);
},
};
}
14 changes: 8 additions & 6 deletions manifest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineManifest } from '@crxjs/vite-plugin'
import packageJson from './package.json'
import 'dotenv/config'
import { defineManifest } from '@crxjs/vite-plugin';
import 'dotenv/config';
import packageJson from './package.json';
const { version } = packageJson


Expand All @@ -20,7 +20,8 @@ export default defineManifest(async (env) => ({
"content_scripts": [
{
"matches": ["https://powerschool.sas.edu.sg/guardian/scores.html*"],
"js": ["src/content_script/scores/index.ts"]
"js": ["src/content_script/scores/index.ts"],

},
{
"matches": ["https://powerschool.sas.edu.sg/*"],
Expand Down Expand Up @@ -48,8 +49,9 @@ export default defineManifest(async (env) => ({
"web_accessible_resources": [
{
"matches": ["https://powerschool.sas.edu.sg/*"],
"resources": ["public/icon.png"]
}
"resources": ["public/icon.png"],
"use_dynamic_url": false
},
],
"background": {
"service_worker": "src/background.ts",
Expand Down
Loading