checkPasteboardAuthorization(conTextSub).then(dic => {
if (dic['status'] == '1') {
let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.getData((err, pasteData) => {
if (err) {
console.error('Failed to get PasteData. Cause: ' + err.message);
return;
} else {
let head = "银行"
let pasteboardstring: string = pasteData.getPrimaryText() as string;
if (pasteboardstring == undefined || pasteboardstring == '') {
console.log('粘贴板内容为空')
return
}
if (pasteboardstring.includes(head)) {
let dict: Record<string, string | object> = {}
dict['result'] = "1"
sendCallbackFail(that, eventId, dict);
pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, '')
console.log('cloudPwdH5 end')
} else {
let dict: Record<string, string | object> = {}
dict['result'] = "0"
sendCallbackFail(that, eventId, dict);
pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, '')
}
let needic: Record<string, Object> = { 'content': pasteboardstring }
if (CommonValues.topH5Component) {
try {
openFuncPage(CommonValues.topH5Component, "cloudPwd", needic) //调起js方法
} catch (error) {
console.log('调用错误' + error.message)
}
} else {
let cloudJS = "var ev=new Event('cloudPwd');ev.param={content:'" + pasteboardstring +
"'};document.getElementById('cloudPwd').dispatchEvent(ev);"
that.webviewController.runJavaScript(cloudJS)
}
}
})
} else if (dic['status'] == '0') {
let atManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(conTextSub, ['ohos.permission.READ_PASTEBOARD']).then(async () => {
let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard();
systemPasteboard.getData((err, pasteData) => {
if (err) {
console.error('Failed to get PasteData. Cause: ' + err.message);
return;
} else {
let head = "杭州银行"
let pasteboardstring: string = pasteData.getPrimaryText() as string;
if (pasteboardstring == undefined || pasteboardstring == '') {
console.log('粘贴板内容为空')
return
}
if (pasteboardstring.includes(head)) {
let dict: Record<string, string | object> = {}
dict['result'] = "1"
sendCallbackFail(that, eventId, dict);
pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, '')
console.log('cloudPwdH5 end')
} else {
let dict: Record<string, string | object> = {}
dict['result'] = "0"
sendCallbackFail(that, eventId, dict);
pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, '')
}
let needic: Record<string, Object> = { 'content': pasteboardstring }
if (CommonValues.topH5Component) {
try {
openFuncPage(CommonValues.topH5Component, "cloudPwd", needic) //调起js方法
} catch (error) {
console.log('调用错误' + error.message)
}
} else {
let cloudJS = "var ev=new Event('cloudPwd');ev.param={content:'" + pasteboardstring +
"'};document.getElementById('cloudPwd').dispatchEvent(ev);"
that.webviewController.runJavaScript(cloudJS)
}
}
})
})
}
})
API 12及之后,系统为提升用户隐私安全保护能力,剪贴板读取接口增加权限管控,申请访问剪贴板权限。参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/get-pastedata-permission-guidelines-V5
示例参考: