I have been banging my head against a wall for the past two days to try and pass an array from my background.js script to my inject.js (content) script but I can't seem to figure it out.
I found this resource on SO that has the same problem: Pass array variable from background.js to content.js
Somehow I still can't seem to figure it out.. It seems he took a different approach than me since I am getting user input to create my array.
I am trying to send the userHash array from background.js to inject.js
Manifest.js
{
"name": "Instagram Automated",
"version": "1.0",
"manifest_version": 2,
"description": "One of the safest Instagram Automation tools on the market.",
"homepage_url": "http://grantdarling.com",
"background": {
"scripts": [
"background.js"
],
"persistent": true
},
"browser_action": {
"default_popup": "popup.html",
"default_title": "Automated Instagram"
},
"permissions": [
"tabs",
"notifications",
"storage",
"http://www.instagram.com/",
"https://www.instagram.com/"
]
}
popup.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="popup.css">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet">
</head>
<body>
<div class="header-container">
<h1 id="title">Automated Instagram</h1>
<img id="insta-logo" src="media/Instagram Logo.svg" />
</div>
<img id="insta-glow" src="media/instagram-glow.svg" />
<div class="content-container">
<p class="sub-paragraph">Search Hashtag</p>
<form>
<input id="hashtagInput" />
<input type="submit" id="addHashtag" id="grabHashtag" value="Submit Hashtag" />
</form>
<div class="divider"></div>
<p>You have liked <span id="count">0</span> photographs</p>
<form id="start-auto-form">
<input type="submit" id="start-auto" value="Start Automation" />
</form>
</div>
<script src="inject.js"></script>
<script src="popup.js"></script>
</body>
</html>
inject.js
/******************************
# Inject into URL on submit
******************************/
document.querySelector('#start-auto').addEventListener("click", findUrl);
function findUrl() {
console.log(userHash)
let hashtagUrl = window.open("https://www.instagram.com/explore/tags/" + userHash );
//*This is the area I am trying to push the array to
}
/*******************************************
# Inject addHashtag array into URL on submit
********************************************/
document.querySelector('#addHashtag').addEventListener("click", addHashtag);
function addHashtag() {
chrome.runtime.sendMessage(hashtagInput.value);
}
background.js
/******************************
# Inject likebot into page
******************************/
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
chrome.tabs.executeScript(tabId, {
file: 'likeBot.js'
}, _ => chrome.runtime.lastError /* "check" error */ )
});
/************************************************************
# Recieve message from likeBot.js and send it to popup.html
************************************************************/
let updateCount = document.getElementById('count');
chrome.runtime.onMessage.addListener(function(response, sender, sendResponse) {
if (updateCount) {
updateCount.innerHTML = response;
}
chrome.storage.sync.set({
'value': response
});
});
/************************************************************
# Recieve message from likeBot
************************************************************/
if (updateCount) {
chrome.storage.sync.get('value', function(data) {
updateCount.innerHTML = data.value;
});
}
/************************************************************
# Grab and store hashtag from input field
************************************************************/
let hashtag = document.querySelector("#hashtagInput");
let userHash = [];
chrome.runtime.onMessage.addListener(function(hashtagInput, sender, sendResponse) {
chrome.storage.sync.set({
list:hashtagInput
}, function() {
});
chrome.storage.sync.get({
list:[] //put defaultvalues if any
},
function(data) {
userHash.push(data.list);
console.log('This is my array: ' + userHash);
sendResponse(hashtagInput);
}
);
sendResponse(userHash);
});
I will be up for another couple of hours trying to figure this out. I would appreciate any help someone could give me. The background.js script successfully creates an array from the user input, I just can't access it from my inject.js script.