javascript显示运行脚本的页面的源代码

javascript show the source of page the that runs the script

本文关键字:源代码 脚本 显示 运行 javascript      更新时间:2023-09-26

我有一个firefox扩展,我想知道如何查看脚本运行的witch文件。
我正在使用window.location.href,但在我的情况下没有那么有用。因为我想知道什么样的文件,例如,我只想让我的脚本在html文件上运行。

我该怎么做?一些想法?

您可以抛出一个Error,捕获它并检查调用堆栈。

var frames = [];
try {
    throw new Error("debug");
} catch (exception) {
    if (exception.stack && typeof exception.stack === "string") {
        var lines = exception.stack.split("'n");
        for (var i = 0; i < lines.length; i += 1) {
            var frame = lines[i].match(new RegExp("^(.*)@(.*):(.*)$"));
            frames.push({
                "function": frame[1] || "anonymous",
                "line": frame[3],
                "file": frame[2]
            });
        }
    }
}
console.log(frames);

已修复:

window.addEventListener('load', function () {
    if (document.doctype) {
        if (document.doctype.name == 'html') {

简单:)无论如何,感谢您的帮助