I want to do the following in this order
1. User clicks button (id='copyDetails') in the popup window
2. Text from specific elements (inc. name field) that are on the users page are stored in a hash and sent to background.js
3. The hash from step 2 is stored in chrome storage
4. Get the name value (which is a key in the hash from step 2) and insert it into element (id='firstName') in popup
The following code does all of the above steps, but in the wrong order (currently does it step 1, 4, 2, 3)
In popup.js
let copyDetails = document.getElementById('copyDetails');
// This gets the most recently stored name and inserts it into the firstName element (works fine)
chrome.storage.sync.get(['key'], function(result) {
document.getElementById('firstName').innerHTML = result.key.firstName;
});
copyDetails.onclick = function(element) {
chrome.extension.getBackgroundPage().console.log("Step 1 - User clicks button");
// the executeScript gets the DOM from the users current page, and extracts the relevant information for the different customer details (inc. name)
chrome.tabs.executeScript({
code: '(' + modifyDOM + ')();'
}, (results) => {
const customerDetails = results[0];
chrome.runtime.sendMessage({customerDetails: customerDetails}, function(response) {
chrome.extension.getBackgroundPage().console.log("Step 2 - send message");
});
});
chrome.storage.sync.get(['key'], function(result) {
chrome.extension.getBackgroundPage().console.log("Step 4 - retrieve name from storage and insert it into popup html element");
document.getElementById('firstName').innerHTML = result.key.firstName;
});
};
function modifyDOM() {
const customerDetails = {
firstName: document.getElementById('customer_first_name').innerHTML,
lastName: document.getElementById('customer_last_name').innerHTML,
email: document.getElementById('customer_email_address').innerText.split('Generate Email')[0].slice(0, -1),
postcode: document.getElementById('customer_uk_postcode').innerHTML
}
return customerDetails
}
Here is the background.js file
function (request, sender, sendResponse) {
chrome.storage.sync.set({key: request.customerDetails}, function() {
chrome.extension.getBackgroundPage().console.log("Step 3 - save details in a hash in storage");
});
});
I think I need to use a callback on the executeScript, but after a few hours of playing around with it I can't get it working.
Definition of done = I press the button (id='copyDetails') and in the background console I see printed Step 1, 2, 3, 4 in that order.
In case you need, popup.html
<!DOCTYPE html>
<html>
<head>
<style>
button {
height: auto;
width: auto;
outline: none;
}
</style>
</head>
<body>
<button id="copyDetails">Copy Details</button>
<div class='details-button'>First Name: <button id="firstName">N/A</button></div>
<script src="popup.js"></script>
</body>
</html>
And my manifest.json
{
"name": "Copy customer details",
"version": "1.0",
"description": "Copy customer details from an rfq with the click of a button!",
"permissions": ["contextMenus", "activeTab", "declarativeContent", "storage"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"page_action": {
"default_popup": "popup.html"
},
"manifest_version": 2
}