Skip to content

Commit c8b5ba1

Browse files
release: 1.0.12 (#364)
Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent b64c3fa commit c8b5ba1

File tree

7 files changed

+928
-903
lines changed

7 files changed

+928
-903
lines changed

fix-csp.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
4+
function updateManifest(distFolder) {
5+
// Define the path to the manifest.json file
6+
const manifestPath = path.join(distFolder, 'manifest.json');
7+
8+
// Find the content.js-YYY file in the dist/assets directory
9+
const assetsFolder = path.join(distFolder, 'assets');
10+
let contentJsFile = null;
11+
12+
// Check if the manifest.json file exists
13+
if (!fs.existsSync(manifestPath)) {
14+
console.log(`Error: manifest.json not found in ${distFolder}.`);
15+
return;
16+
}
17+
18+
// Check if the assets folder exists
19+
if (!fs.existsSync(assetsFolder)) {
20+
console.log(`Error: assets folder not found in ${distFolder}.`);
21+
return;
22+
}
23+
24+
// Iterate over files in the assets folder to find the content.js file
25+
const files = fs.readdirSync(assetsFolder);
26+
for (const filename of files) {
27+
if (
28+
!filename.startsWith('content.js-loader') &&
29+
filename.startsWith('content.js-') &&
30+
filename.endsWith('.js')
31+
) {
32+
contentJsFile = filename;
33+
break;
34+
}
35+
}
36+
37+
// If the content.js file was not found, return with an error message
38+
if (contentJsFile === null) {
39+
console.log('Error: content.js-YYY file not found in the assets folder.');
40+
return;
41+
}
42+
43+
// Read the manifest.json file
44+
let manifestData;
45+
try {
46+
const data = fs.readFileSync(manifestPath, 'utf-8');
47+
manifestData = JSON.parse(data);
48+
} catch (e) {
49+
console.log(`Error reading manifest.json: ${e.message}`);
50+
return;
51+
}
52+
53+
// Update the content_scripts entry in the manifest
54+
let updated = false;
55+
if (manifestData.content_scripts) {
56+
for (const script of manifestData.content_scripts) {
57+
if (script.js) {
58+
// Find any entry matching the pattern content.js-loader-*.js
59+
for (let i = 0; i < script.js.length; i++) {
60+
if (/assets\/content\.js-loader-.*\.js/.test(script.js[i])) {
61+
script.js[i] = `assets/${contentJsFile}`;
62+
updated = true;
63+
break;
64+
}
65+
}
66+
}
67+
}
68+
}
69+
70+
if (!updated) {
71+
console.log('No matching content.js-loader-XXX.js entry found in manifest.json.');
72+
return;
73+
}
74+
75+
// Write the updated manifest.json file back to the file system
76+
try {
77+
fs.writeFileSync(manifestPath, JSON.stringify(manifestData, null, 2));
78+
} catch (e) {
79+
console.log(`Error writing to manifest.json: ${e.message}`);
80+
return;
81+
}
82+
83+
console.log(`Updated manifest.json to use ${contentJsFile} in content_scripts.`);
84+
}
85+
86+
// Run the script
87+
const distFolder = './dist'; // Replace this path with the path to your 'dist' folder
88+
updateManifest(distFolder);

manifest.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 3,
33
"name": "Tab Modifier",
4-
"version": "1.0.11",
4+
"version": "1.0.12",
55
"description": "Take control of your tabs",
66
"homepage_url": "https://github.com/furybee/chrome-tab-modifier",
77
"action": {
@@ -28,7 +28,9 @@
2828
"web_accessible_resources": [{
2929
"resources": [
3030
"assets/*",
31-
"assets/*/*"
31+
"assets/*/*",
32+
"vendor/*",
33+
"vendor/*/*"
3234
],
3335
"matches": [
3436
"<all_urls>"

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "tab-modifier",
33
"private": true,
4-
"version": "1.0.11",
4+
"version": "1.0.12",
55
"type": "module",
66
"scripts": {
77
"test": "vitest --run",
@@ -10,9 +10,10 @@
1010
"coverage": "npx vitest --run --coverage.enabled true",
1111
"dev": "rm -rf ./dist && vite",
1212
"tsc": "vue-tsc",
13-
"build": "rm -rf ./dist && vite build",
13+
"build": "rm -rf ./dist && vite build && yarn fix-csp",
1414
"preview": "vite preview",
15-
"build-zip": "rm -rf ./dist && rm -f tab-modifier.zip && yarn build && cd ./dist && zip -r ../tab-modifier.zip *"
15+
"fix-csp": "node fix-csp.js",
16+
"build-zip": "rm -rf ./dist && rm -f tab-modifier.zip && yarn build && yarn fix-csp && cd ./dist && zip -r ../tab-modifier.zip *"
1617
},
1718
"dependencies": {
1819
"mitt": "^3.0.1",
@@ -23,11 +24,11 @@
2324
"devDependencies": {
2425
"@crxjs/vite-plugin": "^2.0.0-beta.23",
2526
"@types/chrome": "^0.0.268",
26-
"@typescript-eslint/eslint-plugin": "^7.14.1",
27+
"@typescript-eslint/eslint-plugin": "^7.18.0",
2728
"@typescript-eslint/parser": "^7.4.0",
2829
"@vitejs/plugin-vue": "^5.0.5",
29-
"@vitest/coverage-v8": "^1.6.0",
30-
"@vitest/ui": "^1.6.0",
30+
"@vitest/coverage-v8": "^2.0.5",
31+
"@vitest/ui": "^2.0.5",
3132
"@vue/cli-plugin-eslint": "^5.0.8",
3233
"@vue/eslint-config-typescript": "^13.0.0",
3334
"autoprefixer": "^10.4.19",
@@ -42,11 +43,11 @@
4243
"eslint-plugin-vue": "^9.26.0",
4344
"jsdom": "^24.1.0",
4445
"postcss": "^8.4.38",
45-
"prettier": "^3.3.2",
46+
"prettier": "^3.3.3",
4647
"tailwindcss": "^3.4.4",
4748
"typescript": "*",
48-
"vite": "^5.2.0",
49-
"vitest": "^1.6.0",
49+
"vite": "^5.4.2",
50+
"vitest": "^2.0.5",
5051
"vue-eslint-parser": "^9.4.2",
5152
"vue-tsc": "^2.0.24"
5253
}

src/content.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,58 @@
1-
import { _getRuleFromUrl } from './common/storage.ts';
1+
function _getStorageAsync() {
2+
return new Promise((resolve, reject) => {
3+
chrome.storage.local.get(STORAGE_KEY, (items) => {
4+
if (chrome.runtime.lastError) {
5+
reject(new Error(chrome.runtime.lastError.message));
6+
} else {
7+
resolve(items[STORAGE_KEY]);
8+
}
9+
});
10+
});
11+
}
12+
13+
async function _getRuleFromUrl(url) {
14+
const tabModifier = await _getStorageAsync();
15+
if (!tabModifier) {
16+
return;
17+
}
18+
19+
const foundRule = tabModifier.rules.find((r) => {
20+
const detectionType = r.detection ?? 'CONTAINS';
21+
const urlFragment = r.url_fragment;
22+
23+
switch (detectionType) {
24+
case 'CONTAINS':
25+
return url.includes(urlFragment);
26+
case 'STARTS':
27+
case 'STARTS_WITH':
28+
return url.startsWith(urlFragment);
29+
case 'ENDS':
30+
case 'ENDS_WITH':
31+
return url.endsWith(urlFragment);
32+
case 'REGEX':
33+
case 'REGEXP':
34+
return new RegExp(urlFragment).test(url);
35+
case 'EXACT':
36+
return url === urlFragment;
37+
default:
38+
return false;
39+
}
40+
});
41+
42+
if (!foundRule) {
43+
return;
44+
}
45+
46+
return foundRule;
47+
}
248

349
const STORAGE_KEY = 'tab_modifier';
450

5-
export function updateTitle(title, tag, value) {
51+
function updateTitle(title, tag, value) {
652
return value ? title.replace(tag, decodeURI(value)) : title;
753
}
854

9-
export function getTextBySelector(selector) {
55+
function getTextBySelector(selector) {
1056
let el = null;
1157

1258
if (selector.includes('*')) {
@@ -52,7 +98,7 @@ export function getTextBySelector(selector) {
5298
return value.trim();
5399
}
54100

55-
export function processTitle(currentUrl, currentTitle, rule) {
101+
function processTitle(currentUrl, currentTitle, rule) {
56102
let title = rule.tab.title;
57103
const matches = title.match(/\{([^}]+)}/g);
58104

@@ -109,7 +155,7 @@ export function processTitle(currentUrl, currentTitle, rule) {
109155
return title;
110156
}
111157

112-
export function processIcon(newIcon) {
158+
function processIcon(newIcon) {
113159
const icons = document.querySelectorAll('head link[rel*="icon"]');
114160

115161
icons.forEach((icon) => {
@@ -135,7 +181,7 @@ export function processIcon(newIcon) {
135181
return true;
136182
}
137183

138-
export async function applyRule(ruleParam, updateTitle) {
184+
async function applyRule(ruleParam, updateTitle) {
139185
const rule = ruleParam ?? (await _getRuleFromUrl(location.href));
140186
updateTitle = updateTitle ?? true;
141187

vite.config.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
import { defineConfig } from 'vite'
2-
import vue from '@vitejs/plugin-vue'
3-
import { crx, ManifestV3Export } from '@crxjs/vite-plugin'
4-
import manifest from './manifest.json' assert { type: 'json' }
1+
import { defineConfig } from 'vite';
2+
import vue from '@vitejs/plugin-vue';
3+
import { crx, ManifestV3Export } from '@crxjs/vite-plugin';
4+
import manifest from './manifest.json' assert { type: 'json' };
55

66
export default defineConfig({
7-
server: {
8-
strictPort: true,
9-
port: 5173,
10-
hmr: {
11-
clientPort: 5173
12-
}
13-
},
14-
plugins: [
15-
vue(),
16-
crx({ manifest: manifest as unknown as ManifestV3Export }),
17-
]
18-
})
7+
server: {
8+
strictPort: true,
9+
port: 5173,
10+
hmr: {
11+
clientPort: 5173,
12+
},
13+
},
14+
plugins: [vue(), crx({ manifest: manifest as unknown as ManifestV3Export })],
15+
});

0 commit comments

Comments
 (0)