gpt4 book ai didi

javascript - 复制 “Unchecked chrome.runtime.lastError”警告

转载 作者:行者123 更新时间:2023-12-03 08:52:33 24 4
gpt4 key购买 nike

我需要复制chrome.runtime.lastError功能。当自定义函数未捕获chrome.runtime.lastError时,如何触发触发警告?

if(self.chrome == null) {
self.chrome = { runtime: { } }
}

chrome.test = function(option, callback) {
if(option) {
callback("done")
} else {
chrome.runtime.lastError = { message: "Needs something." }
callback()
chrome.runtime.lastError = undefined
} }

chrome.test(false, function(result) {
console.log(result)
})

最佳答案

我能够使用Proxies并覆盖chrome.runtime使其工作。

if(self.chrome == null) {
self.chrome = { runtime: { } }
}

chrome.test = function(option, callback) {
if(option) {
callback("done")
} else {
let error = "Needs something."
let unchecked = true
chrome.oldruntime = chrome.runtime
chrome.runtime = new Proxy(chrome.runtime, {
get(target, key) {
if(key == "lastError") {
unchecked = false
return({ message: error })
} else {
return(target[key])
} } })
callback()
chrome.runtime = chrome.oldruntime
if(unchecked) {
console.error("Unchecked runtime.lastError while running chrome.test: " + error)
} } }

chrome.test(false, function(result) {
console.log(result)
})

关于javascript - 复制 “Unchecked chrome.runtime.lastError”警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37691281/

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