gpt4 book ai didi

javascript - 我们如何将大对象从后台发送到 CrossRider 中的事件选项卡?

转载 作者:行者123 更新时间:2023-11-28 19:27:41 25 4
gpt4 key购买 nike

我们正在使用 CrossRider 开发 Internet Explorer 的扩展。我们的扩展有代码向后台发送消息,后台发送回复并调用回调函数。这在我的装有 Internet Explorer 11 的计算机上有效,但在我 friend Tom 的计算机(也有 Internet Explorer 11)中不起作用 - 在他的计算机中未调用回调。问题是什么?我们如何修复它以使其在任何计算机上工作?相关代码如下:

_base.js:

alert("[ContentBase::getData] >>>>>"); // This happens in any computer.
var request = {command: 'get', webmail: thisObj.mContentType, param: param, type: type, contentType: contentType};
thisObj.sendRequest(request, function(response) {
alert("[ContentBase::getData] received data >>>>>"); // This doesn't happen in Tom's computer.
if (typeof(callback) === 'function') {
callback(response);
}
});

utils.js:

this.sendRequest = function(request, callback) {
if (typeof(callback) !== 'function') {
callback = function(response) {};
}

switch (Sys.platform) {
case 'crossrider':
var message = {request: request, message_id: Math.floor((Math.random() * 900000000000000) + 100000000000000)};
if (typeof thisObj.mCallbackMap === 'undefined') {
thisObj.mCallbackMap = {};
appAPI.message.addListener({channel: "message_from_background"}, function(message) {
if (typeof thisObj.mCallbackMap[message.message_id] === 'function') {
thisObj.mCallbackMap[message.message_id](message.response);
delete thisObj.mCallbackMap[message.message_id];
}
});
}

(function(callback_inner) {
thisObj.mCallbackMap[message.message_id] = function(response) {
if (typeof(callback_inner) === 'function') {
callback_inner(response);
}
};
})(callback);

appAPI.message.toBackground(message, {channel: "message_to_background"});
break;
}
};

background.js:

appAPI.message.addListener({channel: "message_to_background"}, function(params) {
MsgHandler.handle(params.request, undefined, function(responseParams) {
appAPI.message.toActiveTab({'message_id': params.message_id, 'response': responseParams}, {channel: "message_from_background"});
});
});

msgHandler.js:

this.handle = function(request, sender, callback_out) {
function callback(response) {
if (typeof(callback_out) === 'function') {
callback_out(response);
}
}

switch (request.command) {
case "get":
switch (request.type) {
case "all":
var data = Controller.getData();
alert("[MsgHandler::handle] get / all, data.length = " + JSON.stringify(data).length + ", data = " + JSON.stringify(data)); // This happens in any computer.
callback({data: data});
break;
}
break;
}
return true; //this return is needed for chrome in order to execute callbacks

};

Sys.platform 始终等于“crossrider”。

更新:当 JSON.stringify(data).length 为 5981 字节时,消息已被接收,但当为 10157 字节时,主动方未收到消息选项卡(使用 appAPI.message.toActiveTab)。从后台发送的对象大小有什么限制?我们如何将大对象发送到选项卡(最多 100KB)?

我们的扩展 ID 是 43889。我使用的是 Internet Explorer 11,但此扩展应该适用于所有版本的 Internet Explorer。

顺便说一句,来自后台的其他调用可以工作,只有这个特定的调用不起作用。我们在汤姆的计算机上尝试了几次,但始终无法正常工作。

编辑:我创建了一个简单的扩展,也有同样的问题,扩展ID是67708。这是简单扩展的代码:

extension.js:

appAPI.ready(function($) {
alert("appAPI.platform = " + appAPI.platform);
if (appAPI.platform === 'IE') {

appAPI.message.addListener({channel: "message_from_background"}, function(message) {
alert("message_from_background received, message_id = " + message.message_id + ", message.length = " + JSON.stringify(message).length + ", message = " + JSON.stringify(message));
});

appAPI.message.toBackground({}, {channel: "init_background"});
}
});

background.js:

appAPI.ready(function($) {
alert("appAPI.platform = " + appAPI.platform);
if (appAPI.platform === 'IE') {
var ready = false;
appAPI.message.addListener({channel: "init_background"}, function(params) {
if (ready === false) {
alert('init_background, ready = ' + ready);
ready = true;

var message_id = 9999;
var responseParams = {'a': 1, 'b': 2, 'c': 3};
alert('sending message to active tab, message_id = ' + message_id + ', responseParams.length = ' + JSON.stringify(responseParams).length);
appAPI.message.toActiveTab({'message_id': message_id, 'response': responseParams}, {channel: "message_from_background"});

var message_id = 9998;
var responseParams = {
// a big object
};
alert('sending message to active tab, message_id = ' + message_id + ', responseParams.length = ' + JSON.stringify(responseParams).length);
appAPI.message.toActiveTab({'message_id': message_id, 'response': responseParams}, {channel: "message_from_background"});

alert(appAPI.platform);
}
});
}
});

JSON.stringify(responseParams).length为19字节时,事件选项卡会收到消息,但当它为10576字节时,不会收到消息。

最佳答案

@Uri 感谢您更新问题。

根据新信息,我提请您注意文档 ( appAP.message ) 中有关 Internet Explorer 限制的注释:

Messages are converted to JSON strings before they are sent. Due to a limitation in Internet Explorer, the maximum length of the JSON string is 8000 bytes (8Kb).

您可以通过将数据保存在本地数据库并向事件选项卡发送短消息以触发其读取数据来解决此问题。以下是流程的简化示例:

background.js:

appAPI.ready(function($) {
appAPI.db.async.set(
'my-data',
myData,
appAPI.time.minutesFromNow(1),
function() {
appAPI.message.toActiveTab({type: 'get-data'});
}
);
});

extension.js:

appAPI.ready(function($) {
appAPI.message.addListener(function(msg) {
if (msg.type === 'get-data') {
appAPI.db.async.get('my-data', function(data) {
// do something with data
});
}
});
});

[披露:我是 Crossrider 员工]

关于javascript - 我们如何将大对象从后台发送到 CrossRider 中的事件选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27508158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com