Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: GM_getResourceText - binary (backward compatible) #2376

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions modules/miscapis.js
Expand Up @@ -25,10 +25,11 @@ GM_Resources.prototype.getResourceURL = function(aScript, name) {
};


GM_Resources.prototype.getResourceText = function(name) {
GM_Resources.prototype.getResourceText = function(sandbox, name, responseType) {
var dep = this._getDep(name);
if (dep.textContent !== undefined) return dep.textContent;
return GM_util.fileXhr(dep.file_url, "text/plain");
return Cu.cloneInto(GM_util.fileXhr(
dep.file_url, "text/plain", responseType), sandbox);
};

GM_Resources.prototype._getDep = function(name) {
Expand Down
9 changes: 6 additions & 3 deletions modules/sandbox.js
Expand Up @@ -65,7 +65,8 @@ function createSandbox(aScript, aContentWin, aUrl, aFrameScope) {


if (GM_util.inArray(aScript.grants, 'GM_addStyle')) {
sandbox.GM_addStyle = GM_util.hitch(null, GM_addStyle, aContentWin.document);
sandbox.GM_addStyle = GM_util.hitch(
null, GM_addStyle, aContentWin.document);
}
if (GM_util.inArray(aScript.grants, 'GM_log')) {
sandbox.GM_log = GM_util.hitch(new GM_ScriptLogger(aScript), 'log');
Expand Down Expand Up @@ -98,10 +99,12 @@ function createSandbox(aScript, aContentWin, aUrl, aFrameScope) {

var scriptResources = new GM_Resources(aScript);
if (GM_util.inArray(aScript.grants, 'GM_getResourceURL')) {
sandbox.GM_getResourceURL = GM_util.hitch(scriptResources, 'getResourceURL', aScript);
sandbox.GM_getResourceURL = GM_util.hitch(
scriptResources, 'getResourceURL', aScript);
}
if (GM_util.inArray(aScript.grants, 'GM_getResourceText')) {
sandbox.GM_getResourceText = GM_util.hitch(scriptResources, 'getResourceText');
sandbox.GM_getResourceText = GM_util.hitch(
scriptResources, 'getResourceText', sandbox);
}

if (GM_util.inArray(aScript.grants, 'GM_listValues')) {
Expand Down
16 changes: 10 additions & 6 deletions modules/util/fileXhr.js
Expand Up @@ -6,13 +6,17 @@ Components.utils.importGlobalProperties(["XMLHttpRequest"]);

// Sync XHR. It's just meant to fetch file:// URLs that aren't otherwise
// accessible in content. Don't use it in the parent process or for web URLs.
function fileXhr(url, mimetype) {
if (!url.match(/^file:\/\//)) {
throw new Error('fileXhr() used for non-file URL: ' + url + '\n');
function fileXhr(aUrl, aMimetype, aResponseType) {
if (!aUrl.match(/^file:\/\//)) {
throw new Error('fileXhr() used for non-file URL: ' + aUrl + '\n');
}
var xhr = new XMLHttpRequest();
xhr.open("open", url, false);
xhr.overrideMimeType(mimetype);
xhr.open("open", aUrl, false);
if (aResponseType) {
xhr.responseType = aResponseType;
} else {
xhr.overrideMimeType(aMimetype);
}
xhr.send(null);
return xhr.responseText;
return aResponseType ? xhr.response : xhr.responseText;
}