0

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.

Edited by Dani: Formatting fixed

4
Contributors
7
Replies
13
Views
6 Years
Discussion Span
Last Post by Airshow
0

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

0

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.

0

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.

0

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

0

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())

Edited by Troy III: n/a

0

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 :)

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.