I am making a chrome extension that will auto fill a input on a page (the outlook subject line in this case), and I need to somehow pass the message that I get from the input on popup.js (the extension), into the content.js so that I can update the DOM on that page.
GitHub repo here
popup.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<input type="text" id="message" name="message" placeholder="Enter a message..." class="message-input">
<input type="submit" value="Set new message" id="message-btn" class="message-btn">
</div>
</body>
<script src="popup.js"></script>
</html>
popup.js
let messageInput = document.getElementsByClassName('message-input')[0];
let updateMessageBtn = document.getElementById('message-btn');
updateMessageBtn.addEventListener('click', async(messageInput) => {
// Query tab
let queryOptions = { active: true, currentWindow: true };
let tabs = await chrome.tabs.query(queryOptions);
// Open up connection
const port = chrome.tabs.connect(tabs[0].id, {
name: "uiOps",
});
port.postMessage({
message: messageInput.value
});
port.onMessage.addListener(function(msg) {
if (msg.exists) {
alert('Exists');
} else {
alert("Doesn't exist");
}
})
});
content.js
chrome.runtime.onConnect.addListener(function (port) {
port.onMessage.addListener(function (msg) {
if (port.name === "uiOps") {
const idToQuery = msg.message;
if (idToQuery) {
port.postMessage({
exists: true,
});
} else {
port.postMessage({
exists: false,
});
}
}
});
});
manifest.json
{
"name": "Email Autofiller",
"description": "Automatically fill email subject lines!",
"version": "1.0",
"manifest_version": 3,
"permissions": ["storage", "activeTab", "scripting"],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "/images/get_started16.png",
"32": "/images/get_started32.png",
"48": "/images/get_started48.png",
"128": "/images/get_started128.png"
}
},
"icons": {
"16": "/images/get_started16.png",
"32": "/images/get_started32.png",
"48": "/images/get_started48.png",
"128": "/images/get_started128.png"
}
}
I’ve tried a ton of different things, including trying to use localstorage, which didn’t work. I think I’m on the right track, but the Google Chrome Extension docs are confusing me a little. Any help is appreciated – thanks!