I think I'm trying to do something basic, and I hope that someone
points out my flaws. I have written a small chrome extension which I
will post the contents of below. My issue is that in the chrome
browser address var, if I type javascript:alert(localStorage.length)
if returns a number. However from my extension, which calls this
javascript alert, when the script runs, it says '0' meaning nothing is
in the localStorage.
Is there a simple way to fix this in my code? I've searched around
and found multiple reasons, none that I can pin point to be true or
able to fix.
I think my issue is the chrome extension "privacy plugin" runs ffrom
'file:///' which is not the same origin as the localStorage in the
browser.
but how could I circumvent this or fix this. I simply want to use
this extension to see how many keys are in the localstorage, and later
after I fix this, be able to do stuff with the data in the
localStorage.
in the manifest.json
{
"name": "Privacy Plugin",
"version": "1.0",
"description": "The first extension that I made.",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"homepage_url": "https://http://www.truststc.org/",
"browser_action": {
"default_title": "Privacy Plugin",
"default_icon": "Bob_Cool.png",
"popup": "test.html"
}
}
in the test.html:
<!DOCTYPE html>
<html>
<head>
<title>Javascript Window Location Href</title>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
alert(window.localStorage.length);
function listAllItems(){
for(i=0;i<=localStorage.length-1;i++){
key = window.localStorage.key(i);
alert(key);
val = window.localStorage.getItem(key);
alert(val);
}
}
//-->
</script>
</body>
</html>
Thanks in advance!
--
You received this message because you are subscribed to the Google Groups "Chromium HTML5" group.
To post to this group, send email to chromiu...@chromium.org.
To unsubscribe from this group, send email to chromium-html...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-html5/?hl=en.
Thank you Mohamed for your response! I believe I carefully went through the four links and have edited my files. However, when I click my extension, a small little white box appears, but it's not displaying the data in the local storage as expected; nor is it displaying a zero as before. I went through the message passing, and content strips to produce the following files: Is there something wrong with my permissions or matches that causes nothing to popup? Thank you again!//manifest.json
{"name": "Privacy Plugin","version": "1.0","description": "The first extension that I made.","permissions": ["tabs", "http://*/*", "https://*/*"],
"content_stripts": [{"matches": ["tabs", "http://*/*", "https://*/*"],"js":["mika.js"]
}],"homepage_url": "https://http://www.truststc.org/","browser_action": {"default_title": "Privacy Plugin","default_icon": "Bob_Cool.png","popup": "test.html"}}
//test.html
<!DOCTYPE html><html><head><title>Javascript Window Location Href</title></head>
<body onUnload() = "alert(window.localStorage.length)">
<script language="javascript" type="text/javascript"><!--
chrome.browserAction.onClicked.addListener(function(tab) {chrome.tabs.sendRequest(tab.id, {method: "getlocalStorage"}, function(response){listAllItems();});
});function listAllItems(){for(i=0;i<=localStorage.length-1;i++){key = window.localStorage.key(i);alert(key);val = window.localStorage.getItem(key);alert(val);}}//--></script></body></html>
//mika.jschrome.extension.onRequest.addListener(function(request, sender, sendResponse) {console.log(sender.tab ?"from a content script:" + sender.tab.url :"from the extension");if (request.method == "getlocalStorage")sendResponse({data: localStorage.length});elsesendResponse({}); // snub them.});
--____________________________________________________________Mika AyensonWorcester Polytechnic UndergraduateTeam for Research in Ubiquitous Secure Technology (TRUST) at UCBA Spark Inspired to be Great
Thanks Mansour! Just a heads up. I'm not sure how to properly use
this discussion board. I posted a similar topic, and didn't see it
posted, so I thought it wasn't being posted, and I don't know how to
delete it, or where to find it.
Moving on,
Hello Everyone,
Perhaps there is an error with the message passing? As a
user, I should be able to click the extension and receive a alert of
the current local storage. I have followed the tutorials and advice
from several forums but still no luck.
Could someone tell me where I am making the mistake?
Thank you!
//manifest.json
{
"name": "Privacy Plugin",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_title": "Privacy Plugin",
"default_icon": "Bob_Cool.png"
},
"background page": "background.html",
"homepage_url": "https://http://www.truststc.org/",
"content_stripts": [
{
"matches": ["<all_urls>"],
"js":["mika.js"]
}],
"permissions": [
"tabs"
]
}
//mika.js
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.method == "getlocalStorage")
sendResponse({data: localStorage});
else
sendResponse({}); // snub them.
});
//background.html
<!DOCTYPE html>
<html>
<head>
<title>Javascript Window Location Href</title>
</head>
<body onUnload() = "alert(window.localStorage.length)">
<script>
<!--
chrome.tabs.getSelected(null,function(tab) {
chrome.tabs.sendRequest(tab.id, {method: "getlocalStorage"},listAllItems(response.data) {
});
});
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.sendRequest(tab.id, {method: "getlocalStorage"},
listAllItems(response.data){
alert(response.data.length);
});
});
function listAllItems(data){
for(i=0;i<=data.length-1;i++){
key = data.key(i);
alert(data);
val = data.getItem(key);
alert(val);
}
}
//-->
</script>
</body>
</html>
> Looking good so far, just one comment. You have to remember that content
> scripts run in the context of web pages, while extension pages run in their
> own extension context. In the example above that you have provided you
> stated, I see a couple of issues:
>
> 1. chrome.browserAction.onClicked.addListener is not needed because you
> have specified a popup to show in the manifest (popup": "test.html"). You
> can't use both. Your code should change in a way that once you click on that
> browser action the test.html will directly query the current tab and ask the
> content script for its local storage.
> 2. You need to define a "tabs" permission as defined in the docs here
> http://code.google.com/chrome/extensions/tabs.html#manifest, because you
> are using extension message passing.
> 3. You need to get the current tab before sending the request
>
> chrome.tabs.getCurrent(function(tab) {
> chrome.tabs.sendRequest(tab.id, {method: "getlocalStorage"},
> function(response) {
>
> });
> })
>
> 4. When getting a response back from the content script, you are not
> reading the response object. The whole point of using message passing is to
> get a message from the web page context to the extension context. The
> message we are getting in this case is the localStorage length.
>
> listAllItems(response.data);
>
> 5. Your listAllItems, should not call localStorage, remember we said
> calling localStorage from the extension context is different than calling
> localStorage from the web page context. Since we passed in the data through
> the parameter all you do is:
>
> function listAllItems(data) {
> // Do whatever you want.
> alert(data);}
>
> Your content script is correct, you listen for events from the extension
> context and when your extension is requesting "getlocalStorage" your sending
> back the length of that object. I have answered something similar on
> StackOverflow if you would like to take a look:http://stackoverflow.com/questions/3937000/chrome-extension-accessing...
>
> Kind regards,
> Mohamed Mansour
> >>> To post to this group, send email to chromium-ht...@chromium.org.
> >>> To unsubscribe from this group, send email to
> >>> For more options, visit this group at
> >>>http://groups.google.com/a/chromium.org/group/chromium-html5/?hl=en.
>
> > --
> > ____________________________________________________________
> > Mika Ayenson
> > Worcester Polytechnic Undergraduate
> > Team for Research in Ubiquitous Secure Technology (TRUST) at UCB
> > A Spark Inspired to be Great
>
> > michael.ayen...@wpi.edu : email
> > ayen...@eecs.berkeley.edu : email
> >508-507-9072: cell
--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.
--
What you suggested about the onClicked listener makes more sense.
With the code before, it was firing the getCurrent(Tab) as soon as the
extension was reloaded, Now it fires upon an onClicked listener. Now
that I have two listeners in the background, logically, do I need two
listeners in the content script?
Right now both click listeners in
the background.html are sending request with the same method, which
makes me think that in the content script, upon request they will both
try to access the onRequest listener. Is this a problem, or are they
executed at different times?
Also, when I get the current tab, how do I use it? Do I have to do
more with the tab once the method is called? Right now, I;m calling
the current tab, but it seems like im not doing anything with it.
took your advice and looked at the web inspector and found an error
getting the id with the current tab and looked at the actual function
getCurrent which said: 'May be undefined if called from a non-tab
context (for example: a background page or popup view).' so I switched
the function to getSelected() and the error went away!
I'm sure the onClick listener is working in both cases now because the
alerts work when directly in the listener. Once I move the alert in
the function callback of the sendRequest method, it stops working,
which is why I believe there's an error in the message passing.
My logic right now to my understanding is,
1. onClick get the current tab
2. onClick sendRequest to get content script
3. send response of localstorage serialized
4. receive message deseralize
5. manipulate received data
between step 1 and 2, I feel like i'm missing something, but I have
not found many examples to assure me im doing this right.,
Also my manifest does not include a popup.
Thank you!
Mika Ayenson
> ...
>
> read more »