Skip to content

Commit ab8f488

Browse files
authored
Merge pull request #3 from kungfux/feature/inject_content_script_dynamically
Add support for cloud and on-premise instances
2 parents 83ad5e7 + 41c4248 commit ab8f488

File tree

13 files changed

+169
-22
lines changed

13 files changed

+169
-22
lines changed

CONTRIBUTING.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Contributing Guidelines
2+
3+
Thank you for considering contributing to this project! Appreciate your interest and effort in helping to make it better.
4+
5+
## Browser Compatibility
6+
7+
All changes to the code should be tested on Firefox, Chrome, and ideally in Edge to ensure cross-browser compatibility.
8+
9+
We use [webextension-polyfill](https://github.com/mozilla/webextension-polyfill) along with [@types/webextension-polyfill](https://www.npmjs.com/package/@types/webextension-polyfill) to make this happen.
10+
11+
## Code Formatting
12+
13+
We use Prettier to enforce consistent code formatting across the project. Please make sure that your code adheres to the Prettier rules before submitting a pull request.
14+
15+
## Code Quality
16+
17+
We use ESLint to enforce code quality standards. Please make sure that your code passes the ESLint checks before submitting a pull request.
18+
19+
## General Rules
20+
21+
Here are some general rules to follow when contributing to this project:
22+
23+
- Please make sure that your contributions are well-documented and easy to understand
24+
- Avoid committing binary files or dependencies to the repository
25+
- Make sure that your commits are atomic and contain only related changes
26+
- If your contribution is a bug fix, please include a use case to reproduce
27+
- Be respectful and considerate when communicating with other contributors and maintainers
28+
29+
Thank you for your contributions and support for our project!
30+
31+
# Documentation References
32+
33+
- [Firefox Extensions](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions)
34+
- [Chrome Extensions](https://developer.chrome.com/docs/extensions/mv3/)
35+
36+
- [TypeScript](https://www.typescriptlang.org/docs/)
37+
- [WebPack](https://webpack.js.org/concepts/)

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"homepage": "https://github.com/kungfux/jira-issue-expert#readme",
3131
"devDependencies": {
3232
"@eslint-recommended/eslint-config-typescript": "^17.0.3",
33+
"@types/webextension-polyfill": "^0.10.0",
3334
"@typescript-eslint/eslint-plugin": "^5.59.0",
3435
"copy-webpack-plugin": "^11.0.0",
3536
"eslint": "^8.38.0",
@@ -44,5 +45,8 @@
4445
"webpack": "^5.79.0",
4546
"webpack-cli": "^5.0.1",
4647
"webpack-merge": "^5.8.0"
48+
},
49+
"dependencies": {
50+
"webextension-polyfill": "^0.10.0"
4751
}
4852
}

src/background/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Settings } from '../settings';
2+
import { ServiceWorker } from './service-worker';
3+
4+
const settings = new Settings().getSettings();
5+
const serviceWorker = new ServiceWorker(settings.urls);
6+
serviceWorker.init();

src/background/service-worker.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { Tabs, WebNavigation } from 'webextension-polyfill';
2+
import { tabs, webNavigation, scripting } from 'webextension-polyfill';
3+
4+
export class ServiceWorker {
5+
private readonly urls: string[];
6+
private readonly urlRegexs: RegExp[];
7+
8+
public constructor(urls: string[]) {
9+
this.urls = urls;
10+
this.urlRegexs = [];
11+
for (const url of this.urls) {
12+
this.urlRegexs.push(new RegExp(url, 'i'));
13+
}
14+
}
15+
16+
public init(): void {
17+
tabs.onUpdated.addListener((tabId, changeInfo, tabInfo) => {
18+
void this.onTabUpdated(tabId, changeInfo, tabInfo);
19+
});
20+
webNavigation.onCompleted.addListener((details) => {
21+
void this.onNavigationCompleted(details);
22+
});
23+
}
24+
25+
public async onTabUpdated(
26+
tabId: number,
27+
changeInfo: Tabs.OnUpdatedChangeInfoType,
28+
tabInfo: Tabs.Tab
29+
): Promise<void> {
30+
if (changeInfo.url !== undefined && this.isJiraUrl(changeInfo.url)) {
31+
await this.injectContentScript(tabId);
32+
}
33+
}
34+
35+
public async onNavigationCompleted(
36+
details: WebNavigation.OnCompletedDetailsType
37+
): Promise<void> {
38+
if (details.url.length > 0 && this.isJiraUrl(details.url)) {
39+
await this.injectContentScript(details.tabId);
40+
}
41+
}
42+
43+
private isJiraUrl(url: string): boolean {
44+
for (const x of this.urlRegexs) {
45+
if (x.test(url)) {
46+
return true;
47+
}
48+
}
49+
return false;
50+
}
51+
52+
private async injectContentScript(tabId: number): Promise<void> {
53+
try {
54+
await scripting.executeScript({
55+
target: {
56+
tabId,
57+
},
58+
files: ['content.bundle.js'],
59+
});
60+
} catch (error) {
61+
console.error(`Failed to inject content script: ${String(error)}`);
62+
}
63+
}
64+
}

src/content/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { IssueExpert } from './issue-expert';
2+
3+
declare global {
4+
interface Window {
5+
hasRun: boolean;
6+
}
7+
}
8+
9+
(function run() {
10+
if (window.hasRun) {
11+
return;
12+
}
13+
window.hasRun = true;
14+
new IssueExpert().print();
15+
})();

src/content/issue-expert.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class IssueExpert {
2+
print(): void {
3+
console.log('Hello there');
4+
}
5+
}

src/index.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/manifest.chrome.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
{}
1+
{
2+
"background": {
3+
"service_worker": "background.bundle.js",
4+
"type": "module"
5+
}
6+
}

src/manifest.firefox.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"background": {
3+
"scripts": ["background.bundle.js"],
4+
"type": "module"
5+
},
26
"browser_specific_settings": {
37
"gecko": {
48
"id": "{eb6d1100-3fa7-4869-8370-11bc3e058fe1}",

src/manifest.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
"48": "icons/logo48.png",
88
"128": "icons/logo128.png"
99
},
10-
"content_scripts": [
11-
{
12-
"matches": ["*://*.atlassian.net/jira/*"],
13-
"js": ["bundle.js"]
14-
}
15-
],
16-
"host_permissions": ["*://*.atlassian.net/jira/*"]
10+
"permissions": ["tabs", "webNavigation", "scripting", "activeTab"],
11+
"host_permissions": ["<all_urls>"]
1712
}

0 commit comments

Comments
 (0)