I am having an issue whereby I create a RegExp
object in a content script and pass it as part of an object back to the main script using self.port.emit()
. Somewhere along the way it seems to lose its identity as a RegExp and also its toString abilities. The following returns false in the main script, but true in the content script:
Object.prototype.toString.call(regexp) == '[object RegExp]';
regexp instanceof RegExp;
Interestingly for Arrays passed in the same way the following is true:
Object.prototype.toString.call(array) == '[object Array]';
Am I missing something?
What about if you just sent the regex pattern instead of the whole rexexp object? eg encodeURIComponent(regexp.source);
The Add-on SDK doesn't pass objects around when you send messages, only strings - it essentially calls JSON.stringify() on one side and then JSON.parse() on the other. The result is easy to predict:
console.log(JSON.stringify(new RegExp()));
This gives you "{}"
. In other words, JSON.stringify()
treats "custom" objects as normal objects without any properties, object prototypes and such are ignored. What you get in your main code is a plain object, same as if you call new Object()
.
If you need to pass a regular expression to your main code - send regexp.source
, create an actual regular expression on the other side. Sending actual objects around isn't possible.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.