Hi all,

I'm making some changes to a google chrome extension I made and am having some trouble.

Heres my code on a content script page (removeAttr.js) :

chrome.extension.sendRequest({greeting: "whitelist"}, function(response) {
    var whitelist = response.whitelist;
    console.log(response.whitelist);//working
});
alert(whitelist);//alerts "undefined"

How do I acess the whitelist variable from outside the sendrequest() function?

Iv tried saving it to a window.var variable with no luck. Iv tried creating a div and assigning it's innerHTML as the whitelist variable and getting it later with no luck.

The fact that it's a chrome extension complicates things because i dont actually know if i can create elements from where the script is located.

Can anybody help me?

Max.

Hi all,

I'm making some changes to a google chrome extension I made and am having some trouble.

Heres my code on a content script page (removeAttr.js) :

chrome.extension.sendRequest({greeting: "whitelist"}, function(response) {
	var whitelist = response.whitelist;
	console.log(response.whitelist);//working
});
alert(whitelist);//alerts "undefined"

How do I acess the whitelist variable from outside the sendrequest() function?

Iv tried saving it to a window.var variable with no luck. Iv tried creating a div and assigning it's innerHTML as the whitelist variable and getting it later with no luck.

The fact that it's a chrome extension complicates things because i dont actually know if i can create elements from where the script is located.

Can anybody help me?

Max.

to use a variable outside a function you need to declare the variable outside a function and run that function.

this short tutorial should help.

PS. please wrap you code with the CODE tags so that it's more easier to read. thanks

Hi,

Thanks for the reply. I know about variable scope, i just needed help getting round it.

In the end I just put the rest of my code for the page inside the same function, it required quite a lot of changes to the script but it's all I could do to work around it.

you're welcome, I don't think I'll be much help anymore, my knowledge in OOP in javascript is severely minimal. sorry and good luck.

Max,

Do exactly what qazplm said.

var whitelist; 

chrome.extension.sendRequest({greeting: "whitelist"}, function(response) {
	whitelist = response.whitelist;
	console.log(response.whitelist);//working
});

alert(whitelist);//alerts "undefined"

Airshow

Hi all,

I'm making some changes to a google chrome extension I made and am having some trouble.

Heres my code on a content script page (removeAttr.js) :

chrome.extension.sendRequest({greeting: "whitelist"}, function whtlst(response) {
var whitelist = response.whitelist;
console.log(response.whitelist);//working
});
alert(whitelist);//alerts "undefined"

How do I acess the whitelist variable from outside the sendrequest() function?

Iv tried saving it to a window.var variable with no luck. Iv tried creating a div and assigning it's innerHTML as the whitelist variable and getting it later with no luck.

The fact that it's a chrome extension complicates things because i dont actually know if i can create elements from where the script is located.

Can anybody help me?

Max.

local variables closures or private variables are ment not to be able to access them globaly but a workaround would be to return the one you need...

chrome.extension.sendRequest({greeting: "whitelist"}, function whtlst(response) {
	var whitelist = response.whitelist;
	console.log(response.whitelist);//working
return whitelist});
//alert(whitelist);//alerts "undefined"

alert(whtlst())

Ahhh of course, didnt think of that! :) I'll do that in the future. Thanks everybody for the help - it's been a while since I'v used javascript and maintained my chrome extensions :)

Inner functions have access to all outer members (and outer-outer, outer-outer-outer, etc. etc. right up to global members) providing their names have not been overridden by a var declaration at any stage in the scope nesting.

This is one of the basic tenets of javascript that must be grasped if we wish to avoid name conflicts and to realise the benefit of closures.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.