问题
I am trying to write a web extension where I need to pass an object from the content script to the page script. I have looked at, among others, https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#Sharing_content_script_objects_with_page_scripts and https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.cloneInto
I want to use the cloneInto function described there. There is a code example that looks like this:
// this happens in the content script
var messenger = {
notify: function(message) {
browser.runtime.sendMessage({
content: "Object method call: " + message
});
}
};
window.wrappedJSObject.messenger = cloneInto(
messenger,
window,
{cloneFunctions: true});
Then in the page script, this:
window.messenger.notify("Message from the page script!");
When I paste this into my own extension, by the time it gets to the above line in the page script an error is reported: TypeError: window.messenger is undefined.
I cannot figure out how to solve this. Any thoughts? Or is there a better way to share an object from the content script with the page script?
回答1:
I found an alternative solution. I didn't solve the question of how to use cloneInto but I am able to pass objects to my page script now, by using sendMessage
and creating an onMessage
listener.
Content script:
browser.runtime.sendMessage({theBlobs: myBlobObjects});
Page script:
browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
var blobs = request.theBlobs;
for (var i = blobs.length - 1; i >= 0; i--) {
// do clever things with the blobs
}
}
An advantage of this approach is that it doesn't use Firefox-specific functions.
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/sendMessage https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/onMessage
回答2:
I avoid using cloneInto() and exportFunction(), when possible
The functions cloneInto()
and exportFunction()
are Firefox-only. One of the significant advantages of WebExtensions is the ability to write extensions which work in multiple browsers. Thus, I prefer to avoid using those functions, where possible.
What I would do instead, is use a utility function I wrote, executeInPage(), to copy the Object, Array, function, RegExp, Date, and/or other primitives (Boolean, null, undefined, Number, String, but not Symbol) into the page. Another simple function inPageAssignToWindowProperty()
can assign the value passed to a variable in the global scope.
This is a somewhat more limited than cloneInto()
, as executeInPage()
does not, currently, support: Blob, File, FileList, ArrayBuffer, ArrayBufferView, ImageData, Map, and Set. These are not currently supported, as I have not had a need to pass them into the page context.
var messenger = {
notify: function(message) {
browser.runtime.sendMessage({
content: "Object method call: " + message
});
}
};
function inPageAssignToWindowProperty(property, value) {
window[property] = value;
}
executeInPage(inPageAssignToWindowProperty, false, '', 'messenger', messenger);
console.log('window.messenger:', window.messenger);
<script>
/* executeInPage takes a function defined in this context, converts it to a string
* and inserts it into the page context inside a <script>. It is placed in an IIFE and
* passed all of the additional parameters passed to executeInPage.
* Parameters:
* func The function which you desire to execute in the page.
* leaveInPage If this does not evaluate to a truthy value, then the <script> is
* immediately removed from the page after insertion. Immediately
* removing the script can normally be done. In some corner cases,
* it's desirable for the script to remain in the page. However,
* even for asynchronous functionality it's usually not necessary, as
* the context containing the code will be kept with any references
* (e.g. the reference to a callback function).
* id If this is a non-blank string, it is used as the ID for the <script>
* All additional parameters are passed to the function executing in the page.
* This is done by converting them to JavaScript code-text and back.
* All such parameters must be Object, Array, functions, RegExp,
* Date, and/or other primitives (Boolean, null, undefined, Number,
* String, but not Symbol). Circular references are not supported.
* If you need to communicate DOM elements, you will need to
* pass selectors, or other descriptors of them (e.g. temporarily
* assign them a unique class), or otherwise communicate them to the
* script (e.g. you could dispatch a custom event once the script is
* inserted into the page context).
*/
function executeInPage(functionToRunInPage, leaveInPage, id) {
//Execute a function in the page context.
// Any additional arguments passed to this function are passed into the page to the
// functionToRunInPage.
// Such arguments must be JSON-ifiable (also Date, Function, and RegExp) (prototypes
// are not copied).
// Using () => doesn't set arguments, so can't use it to define this function.
// This has to be done without jQuery, as jQuery creates the script
// within this context, not the page context, which results in
// permission denied to run the function.
function convertToText(args) {
//This uses the fact that the arguments are converted to text which is
// interpreted within a <script>. That means we can create other types of
// objects by recreating their normal JavaScript representation.
// It's actually easier to do this without JSON.strigify() for the whole
// Object/Array.
var asText = '';
var level = 0;
function lineSeparator(adj, isntLast) {
level += adj - ((typeof isntLast === 'undefined' || isntLast) ? 0 : 1);
asText += (isntLast ? ',' : '') +'\n'+ (new Array(level * 2 + 1)).join('');
}
function recurseObject(obj) {
if (Array.isArray(obj)) {
asText += '[';
lineSeparator(1);
obj.forEach(function(value, index, array) {
recurseObject(value);
lineSeparator(0, index !== array.length - 1);
});
asText += ']';
} else if (obj === null) {
asText +='null';
//undefined
} else if (obj === void(0)) {
asText +='void(0)';
//Special cases for Number
} else if (Number.isNaN(obj)) {
asText +='Number.NaN';
} else if (obj === 1/0) {
asText +='1/0';
} else if (obj === 1/-0) {
asText +='1/-0';
//function
} else if (obj instanceof RegExp || typeof obj === 'function') {
asText += obj.toString();
} else if (obj instanceof Date) {
asText += 'new Date("' + obj.toJSON() + '")';
} else if (typeof obj === 'object') {
asText += '{';
lineSeparator(1);
Object.keys(obj).forEach(function(prop, index, array) {
asText += JSON.stringify(prop) + ': ';
recurseObject(obj[prop]);
lineSeparator(0, index !== array.length - 1);
});
asText += '}';
} else if (['boolean', 'number', 'string'].indexOf(typeof obj) > -1) {
asText += JSON.stringify(obj);
} else {
console.log('Didn\'t handle: typeof obj:', typeof obj, ':: obj:', obj);
}
}
recurseObject(args);
return asText;
}
var newScript = document.createElement('script');
if(typeof id === 'string' && id) {
newScript.id = id;
}
var args = [];
//using .slice(), or other Array methods, on arguments prevents optimization
for(var index=3;index<arguments.length;index++){
args.push(arguments[index]);
}
newScript.textContent = '(' + functionToRunInPage.toString() + ').apply(null,'
+ convertToText(args) + ");";
(document.head || document.documentElement).appendChild(newScript);
if(!leaveInPage) {
//Synchronous scripts are executed immediately and can be immediately removed.
//Scripts with asynchronous functionality of any type must remain in the page
// until complete.
document.head.removeChild(newScript);
}
return newScript;
};
</script>
来源:https://stackoverflow.com/questions/46670197/how-to-use-cloneinto-in-a-firefox-web-extension