Последние два дня я бился головой о стену, пытаясь передать массив из моего фона. Скрипт js в мой инъект. Скрипт js (content), но я не могу понять, это из.
Я нашел этот ресурс в SO с такой же проблемой: Передать переменную массива из фона. js в контент. js
Каким-то образом я все еще могу ' Я, кажется, не понял этого. Кажется, он использовал другой подход, чем я, поскольку я получаю пользовательский ввод для создания массива.
Я пытаюсь отправить массив userHa sh из фона. js для инъекции. js
Манифест. 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/"
]
}
всплывающее окно. 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>
инъекция. 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);
}
фон. 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);
});
Я буду на другую пару часов, пытаясь выяснить это. Я был бы признателен за любую помощь, которую кто-то может мне дать. Фоновый сценарий. js успешно создает массив из пользовательского ввода, я просто не могу получить к нему доступ из своего сценария inject. js.