Access/load/read local extension file in content script

2,263 views
Skip to first unread message

Andy Mitchell

unread,
Feb 15, 2010, 6:14:11 AM2/15/10
to Chromium-extensions
Hi guys,

I'm trying to read a chrome extension file (chrome://gtdinbox/
testfile.txt) from within a content script (in the same extension).

In particular, it would be fantastic to be able to do this
synchronously because the depending code is then much more
maintainable.

The core function is built around the following XHR:
var req = new XMLHttpRequest();
req.open('GET', filename, false);
req.send(null);
if( req.status==0 ) return req.responseText;

So far, I tried,
1) Synchronous XHR from content script: failed due to permissions.
2) Synchronous XHR from background page: worked, but has to use Chrome
port message passing to return the data to the content script, which
is asynchronous.
3) A kludge of iframe/script elements to load the data, but all are
asynchonrous.

Am I missing something trivial?

A related question is, are there any plans for synchronous message
passing in Chrome?


Andy

PhistucK

unread,
Feb 15, 2010, 2:08:13 PM2/15/10
to Andy Mitchell, Chromium-extensions
Content scripts run in (sort of) the context of the page (but in a different world - which is a very important difference), so maybe that is why you cannot make a cross origin (page to extension, so to speak) request.
Other than that, did you put the absolute path as "filename"? did you try to use chrome.extension.getURL("filename")?
If they do not work, that is probably intentional, anyway.

Doing it in the background page and sending a message to the content script is the advised way (if the request does not work in the content script after following my suggestion above with getURL)

☆PhistucK



--
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.


Andy Mitchell

unread,
Feb 15, 2010, 2:51:40 PM2/15/10
to Chromium-extensions
Thanks PhistucK - I think it is intentional that content scripts can't
read local files via XHR (as it's cross-origin). Although that does
raise another interesting question -- as I believe the content script
is securely isolated from the page's real DOM, why does it have such
stringent security imposed upon it too?


I've just spotted what looks like a similar thread:
http://groups.google.com/a/chromium.org/group/chromium-extensions/browse_thread/thread/7d90e4c50f5e3dea/ff9053ecfd002c55
I don't want to duplicate anything that gets answered elsewhere.

> > To post to this group, send email to chromium-extensi...@chromium.org.


> > To unsubscribe from this group, send email to

> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>

Reply all
Reply to author
Forward
0 new messages