Skip to content

Conversation

@g8up
Copy link
Contributor

@g8up g8up commented Oct 1, 2025

统一返回值数据结构,以便向下正常透传,供交互反馈正确解析。

解释

  • 视图层触发上传、下载逻辑时,与背景页交互,期待的返回结果是
// ret
{
  message: 'xxx', // 'done' 或其他值
  note: 'xxx',
}

async function test(action=browser.i18n.getMessage('test'))
{
console.log("request,begin");
if( !data['endpoint'] || !data['password'] || !data['uuid'] || !data['type'] )
{
alert(browser.i18n.getMessage("fullMessagePlease"));
return;
}
if( data['type'] == 'pause' )
{
// alert('暂停状态不能'+action);
alert(browser.i18n.getMessage("actionNotAllowedInPause"));
return;
}
const ret = await sendToBackground<RequestBody, ResponseBody>({name:"config",body:{payload:{...data,no_cache:1}}});
console.log(action+"返回",ret);
if( ret && ret['message'] == 'done' )
{
if( ret['note'] )
alert(ret['note']);
else
alert(action+browser.i18n.getMessage('success'));
}else
{
alert(action+browser.i18n.getMessage('failedCheckInfo'));
}
}

  • 背景页响应时,会有个字段名的映射转换: action对应message

    export const handler: PlasmoMessaging.MessageHandler<RequestBody,
    ResponseBody> = async (req, res) => {
    // 获得cookie,并进行过滤
    const payload = req.body.payload;
    const result = (payload['type'] && payload['type'] == 'down') ? await download_cookie(payload) : await upload_cookie(payload);
    res.send({
    message: result['action'],
    note: result['note'],
    })
    }

  • 本次修改将upload_cookie()download_cookie()的异常时的返回值数据结构统一成:

{
  action: 'xxx',
  note: 'xxx'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant