3. Как общаться из background.js в popup.js
Один из вариантов через storage:
manifest.js"permissions": [
"storage"
]
background.jsfunction getUpdates()
{
$.ajax({
url: server + '/api/updates',
success: function(data){
setStorage('updates', data);
}
});
}
function setStorage(key, value)
{
chrome.storage.local.set({[key]: value}, function() {
console.log('Value is set to ' + value);
});
}
popup.jsfunction getStorage(key) {
var ar = [];
var data = '';
ar.push(key);
return new Promise(function(resolve, reject) {
chrome.storage.local.get(key, function(obj) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
reject(chrome.runtime.lastError.message);
} else {
resolve(obj[ar[0]]);
}
});
});
}
getStorage('updates').then(function(item) {
console.log(item);
});;