So I want to respond to a request with data pulled from storage. Not sure why this code works but anyway any idea how I can remove the alert under the comment so that I will not have an empty pop up and this still work.
background.js:
var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.command == "submit-chat") {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
});
} else if (request.command == "set-emots"){
var data = {};
request.emots.forEach(function(entry) {
var emot = JSON.parse(entry);
data[emot.alt] = JSON.stringify(entry);
});
chrome.storage.sync.clear();
chrome.storage.sync.set(data);
} else if (request.command == "get-emots"){
//Not sure why the alert is needed it is like it lets the sendResponce out of the function
alert(chrome.storage.sync.get(null, function(result){
if (!result) {
getPage();
} else {
Object.keys(result).forEach(function(entry) {
mData.push(result[entry]);
});
}
sendResponse({emots: mData.join("|≈|")});
}));
}
});
Where it is being called
content.js
var emoticons = null;
window.onload = function(){
jQuery.noConflict();
chrome.runtime.sendMessage({command: "get-emots"}, function(response) {
emoticons = response.emots;
if (emoticons !== null) {
var input = document.getElementsByClassName("input")[0].innerHTML = "";
jQuery(".input").load(chrome.extension.getURL("input.html"), function(){...});
}
});
}
UPDATE: So I tried this but still a no go
var getEmots = function(callback){
chrome.storage.sync.get(null, function(result){
if (!result) {
getPage();
} else {
Object.keys(result).forEach(function(entry) {
mData.push(result[entry]);
});
}
typeof callback === 'function' && callback(mData);
});
};
var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.command == "submit-chat") {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
});
} else if (request.command == "set-emots"){
var data = {};
request.emots.forEach(function(entry) {
var emot = JSON.parse(entry);
data[emot.alt] = JSON.stringify(entry);
});
chrome.storage.sync.clear();
chrome.storage.sync.set(data);
} else if (request.command == "get-emots"){
getEmots(function(mData){
console.log("Data: " + mData);
sendResponse({emots: mData.join("|≈|")});
});
}
});
UPDATE 2: Got it working here is the final for anybody else. Thanks everyone here is the final on Github
This is background.js
function getPage(){
alert("You are going to be redirected to a page to get all hipchat emoticons. Please refresh that page then come back and refresh this page.");
var win = window.open('http://hipchat-emoticons.nyh.name', '_blank');
win.focus();
};
var getEmots = function(callback){
chrome.storage.sync.get(null, function(result){
if (!result) {
getPage();
} else {
Object.keys(result).forEach(function(entry) {
mData.push(result[entry]);
});
}
typeof callback === 'function' && callback(mData);
});
};
var mData = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if (request.command == "submit-chat") {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.executeScript(tab.id, {code: "chat.send_message();"});
});
} else if (request.command == "set-emots"){
var data = {};
request.emots.forEach(function(entry) {
var emot = JSON.parse(entry);
data[emot.alt] = JSON.stringify(entry);
});
chrome.storage.sync.clear();
chrome.storage.sync.set(data);
} else if (request.command == "get-emots"){
getEmots(function(mData){ sendResponse({emots: mData.join("|≈|")}); });
return true;
}
});