Skip to content

Commit c1349fd

Browse files
committedSep 8, 2024
my very first chrome extension using activeTab
0 parents  commit c1349fd

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed
 

‎README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Page Redder
2+
3+
This sample demonstrates how to use the [chrome.action](https://developer.chrome.com/docs/extensions/reference/api/action) API to execute code when the extension icon is clicked.
4+
5+
## Overview
6+
7+
This extension changes the background color of the active tab page (if its URL doesn't start with `chrome://`) to red when the extension icon is clicked.
8+
9+
## Running this extension
10+
11+
1. Clone this repository.
12+
2. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
13+
3. Navigate to any page (make sure that the URL doesn't start with `chrome://`).
14+
4. Click the extension icon.

‎content.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const port = chrome.runtime.connect({name: "knockknock"});
2+
3+
// Article DropDown Number (important as far as driving the logic, even if the serial itself is not useful)
4+
const driving_field = '#form-field-name'
5+
6+
const fields_pii = {
7+
// Email
8+
email: '#form-field-field_73e261c',
9+
// First Name
10+
first_name: '#first_name',
11+
// Last Name
12+
last_name: '#last_name'
13+
}
14+
15+
const fields_affected_by_driving_field = {
16+
// Title
17+
title: '#form-field-field_8d95f46',
18+
// URL
19+
url: '#form-field-field_11694e0',
20+
// Date
21+
publication_date: '#form-field-field_d6dc6b3',
22+
// Channel
23+
channel: '#form-field-field_c6c1572',
24+
// Channel email
25+
channel_email: '#form-field-field_296556d',
26+
// Subject
27+
subject: '#form-field-field_5547412',
28+
// Dear Email (not necessary, static)
29+
// '#dear_email',
30+
// Complaint description
31+
complaint_descriptiom: '#form-field-field_9f63b1b'
32+
}
33+
34+
document.querySelector('#scroll-to-form-btn').addEventListener('click', addEventListenersForAllFields)
35+
36+
function addEventListenersForAllFields() {
37+
document
38+
.querySelector(driving_field)
39+
.addEventListener('change',function(event) {
40+
console.log(driving_field, "changed to", event.target.value)
41+
console.log("getting initial values on select")
42+
// TODO: if the website can detect that the user
43+
// has installed the extension, we can make this
44+
// better, otherwise, fields on initial load
45+
// are being missed. Here, I have put a random
46+
// 1 second wait to avoid that from happening
47+
// it seems to work fine. We can't wait too long
48+
// or else we may compete with user's actual change detection
49+
setTimeout(() => {
50+
Object.keys(fields_affected_by_driving_field).forEach(field => {
51+
console.log(field)
52+
const sel = document.querySelector(fields_affected_by_driving_field[field])
53+
console.log(sel.value)
54+
port.postMessage({key: field, value: sel.value, initial: true})
55+
});
56+
}, 1000)
57+
});
58+
59+
addEventListenerForFields(fields_pii)
60+
addEventListenerForFields(fields_affected_by_driving_field)
61+
}
62+
63+
function addEventListenerForFields(fields_map) {
64+
Object.keys(fields_map).forEach(field => {
65+
console.log(field, "is being listened to for changes")
66+
document
67+
.querySelector(fields_map[field])
68+
.addEventListener('change',function(event) {
69+
console.log(field, "changed to", event.target.value)
70+
port.postMessage({key: field, value: event.target.value});
71+
});
72+
})
73+
}
74+

‎extension.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
console.log('extension.js')
2+
3+
chrome.runtime.onConnect.addListener(function(port) {
4+
console.assert(port.name === "knockknock");
5+
port.onMessage.addListener(function(msg) {
6+
console.log('is_initial', msg.initial)
7+
console.log(msg.key)
8+
console.log(msg.value)
9+
});
10+
});

‎manifest.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Page Redder",
3+
"manifest_version": 3,
4+
"version": "0.1",
5+
"description": "Turns the page red when you click the icon",
6+
"permissions": ["activeTab", "scripting"],
7+
"content_scripts": [{
8+
"js": ["content.js"],
9+
"matches": ["*://some-website/*"],
10+
"runAt": "document_idle"
11+
}],
12+
"background": {
13+
"service_worker": "service-worker.js",
14+
"type": "module"
15+
}
16+
}

‎service-worker.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './extension.js';

0 commit comments

Comments
 (0)
Please sign in to comment.