I've successfully retrieved data from a website, but I'm struggling to send it to popup.js. I attempted to use executeScript with a callback, but it's not working for me.
popup.js
function getData() {
chrome.tabs.query({active: true, currentWindow: true},function(tabs) {
chrome.tabs.executeScript(tabs[0].id,{
file: 'getdata.js'
});
});
}
document.getElementById('clickme').addEventListener('click', getData);
function loadData() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.update(tabs[0].id, {url: tabs[0].url});
chrome.tabs.executeScript(tabs[0].id,{
file: 'loaddata.js'
});
});
}
document.getElementById('clickme2').addEventListener('click', loadData);
getdata.js
var div = document.querySelectorAll('div');
var array = [];
div.forEach(function(item) {
item.getAttribute('data');
array.push(item.getAttribute('data'));
});
console.log(array);
loaddata.js
var div = document.querySelectorAll('div');
div.forEach(function(item, i){
item.setAttribute('data', array[i]);
});
manifest.json
{
"manifest_version": 2,
"name": "aa",
"description": "bb",
"version": "1.0",
"icons": {
"48": "icon.png"
},
"permissions": ["tabs", "<all_urls>", "contentSettings", "contextMenus", "unlimitedStorage"],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"js": ["jquery-3.2.1.min.js", "popup.js"]
}
}
}
EDIT: 1. I successfully retrieved data using executeScript after adding array at the end of getdata.js.
- How can I send an array from popup.js/getdata.js to loaddata.js?