簡體   English   中英

從chrome擴展內容腳本拋出調試

[英]throwing a debug from chrome extension content script

簡潔版本

嘗試編寫一個調試命令以返回調用堆棧,減去當前位置。 我以為我會用:

try {
    throw new Error(options["msg"])
} catch (e) {
    e.stack.shift;
    throw (e);
}

但我不知道該怎么做。 顯然我不能只是像e.stack.shift這樣。 同樣,這總是使它成為Uncaught Error -但是這些應該只是調試消息。

長版

我決定需要一個用於內容腳本的調試庫。 這里是:

debug.js

var debugKeys = {
    "level": ["off", "event", "function", "timeouts"],
    "detail": ["minimal", "detailed"]
};
var debugState = { "level": "off", "detail": "minimal" };

function debug(options) {
    if ("level" in options) {
        if (verifyDebugValue("level", options["level"]) == false)
            return
    }
    if ("detail" in options) {
        if (verifyDebugValue("detail", options["detail"]) == false)
            return
    }

    console.log(options["msg"]);
}

function verifyDebugValue(lval, rval){
    var state = 10; // sufficiently high
    for (k in debugKeys[lval]) {
        if (debugKeys[lval][k] == rval) {
            return true;
        }
        if (debugKeys[lval][k] == debugState[lval]) { // rval was greater than debug key
            return false;
        }
    }
}

使用它時,可以根據需要更改代碼中的debugState 這項工作仍在進行中,但效果很好。

要從另一個內容腳本中使用它,只需將其加載到清單中,如下所示:

的manifest.json

  "content_scripts": [
    {
      "js": ["debug.js", "foobar.js"],
    }
  ],

然后將其命名為:

debug({"level": "timeouts", "msg": "foobar.js waitOnElement() timeout"});

產生:

foobar.js waitOnElement() timeout debug.js:17

還有我的問題。 目前,它正在使用控制台日志,因此所有調試語句都來自同一debug.js行。 我寧願返回調用上下文。 我想我需要像這樣的東西:

try {
    throw new Error(options["msg"])
} catch (e) {
    e.stack.shift;
    throw (e);
}

但我不知道該怎么做。 顯然我不能只是像e.stack.shift這樣。 同樣,這總是使它成為Uncaught Error -但是這些應該只是調試消息。

您無法避免在debug.js提到這一行,因為使用throw (...)console.log/error(...)您的debug.js將發出命令。

您可以做的是在代碼中包含一些try-catch塊,然后在catch塊中將錯誤對象傳遞給您的調試函數,調試函數將根據其debugState對其進行debugState

在任何情況下,都不清楚如何使用調試庫(以及為什么需要從堆棧跟蹤中刪除最后一個調用,但是您可以嘗試如下操作:

  1. 將堆棧跟蹤(實際上是多行字符串)分成幾行。
  2. 隔離不屬於錯誤消息的第一行(對應於最后一次調用)。
  3. 放一條新的堆棧跟蹤,並刪除行。

例如:

function removeLastFromStack(stack, errMsg) {
    var firstLines = 'Error: ' + errMsg + '\n';
    var restOfStack = stack
            .substring(firstLines.length)   // <-- skip the error's message
            .split('\n')                    // <-- split into lines
            .slice(1)                       // <-- "slice out" the first line
            .join('\n');                    // <-- put the rest back together
    return firstLines + restOfStack;
}

function myDebug(err) {
    /* Based on my `debugState` I should decide what to do with this error.
     * E.g. I could ignore it, or print the message only, 
     * or print the full stack-trace, or alert the user, or whatever */
    var oldStack = err.stack;
    var newStack = removeLastFromStack(oldStack, err.message);
    console.log(newStack);
    //or: console.error(newStack);
}

/* Somewhere in your code */
function someFuncThatMayThrowAnErr(errMsg) {
    throw new Error(errMsg);
}

try {
    someFuncThatMayThrowAnErr('test');
} catch (err) {
    myDebug(err);
}

...但是我仍然看不到從跟蹤中刪除最后一個呼叫有什么幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM