Skip to content

Commit 516c165

Browse files
committed
Add background script to notify when URL changes
1 parent b96772a commit 516c165

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
node_modules
2-
dist/bundle.js
2+
dist/*.bundle.js

dist/manifest.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
"manifest_version": 2,
33
"name": "My Cool Extension",
44
"version": "0.1",
5+
"background": {
6+
"scripts": ["background.bundle.js"]
7+
},
58
"content_scripts": [
69
{
710
"matches": ["<all_urls>"],
811
"js": [
912
"jquery-3.4.1.slim.min.js",
10-
"bundle.js"
13+
"content.bundle.js"
1114
]
1215
}
16+
],
17+
"permissions": [
18+
"tabs"
1319
]
1420
}

rollup.config.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import json from '@rollup/plugin-json';
22

3-
export default {
3+
export default [{
44
input: 'src/index.js',
55
output: {
6-
file: 'dist/bundle.js'
6+
file: 'dist/content.bundle.js'
77
},
88
plugins: [json()]
9-
};
9+
}, {
10+
input: 'src/background.js',
11+
output: {
12+
file: 'dist/background.bundle.js'
13+
}
14+
}];

src/background.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import messages from './messages.js';
2+
3+
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
4+
if (changeInfo.url) {
5+
chrome.tabs.sendMessage( tabId, {
6+
message: messages.URL_CHANGE,
7+
url: changeInfo.url
8+
});
9+
}
10+
});

src/index.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import plugins from './plugins/index.js';
22
import { version } from '../package.json';
3+
import messages from './messages.js';
34

4-
const url = document.URL;
5-
6-
for (let plugin of plugins) {
7-
if (plugin.runFor(url)) {
8-
console.group(`Josh's extension v${version}`);
9-
console.log(`Running '${plugin.name}'...`)
10-
plugin.run();
11-
console.groupEnd(`Josh's extension`);
5+
const runPlugins = () => {
6+
const url = document.URL;
7+
for (let plugin of plugins) {
8+
if (plugin.runFor(url)) {
9+
console.group(`Josh's extension v${version}`);
10+
console.log(`Running '${plugin.name}'...`)
11+
plugin.run();
12+
console.groupEnd(`Josh's extension v${version}`);
13+
}
1214
}
1315
}
16+
runPlugins();
17+
18+
chrome.runtime.onMessage.addListener(
19+
function(request, sender, sendResponse) {
20+
// listen for messages sent from background.js
21+
if (request.message === messages.URL_CHANGE) {
22+
runPlugins();
23+
}
24+
});

src/messages.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
URL_CHANGE: 'url-change'
3+
};

0 commit comments

Comments
 (0)