[英]How do I get the path of the currently running script with Javascript?
We have an IE extension implemented as a Browser Helper Object (BHO). 我们将IE扩展实现为浏览器帮助对象(BHO)。 We have a utility function written in C++ that we add to the window object of the page so that other scripts in the page can use it to load local script files dynamically.
我们有一个用C ++编写的实用程序函数,我们将其添加到页面的窗口对象中,以便页面中的其他脚本可以使用它来动态加载本地脚本文件。 In order to resolve relative paths to these local script files, however, we need to determine the path of the JavaScript file that calls our function:
但是,为了解析这些本地脚本文件的相对路径,我们需要确定调用函数的JavaScript文件的路径:
myfunc()
written in C++ and exposed to the page's JavaScript myfunc()
用C ++编写并暴露给页面的JavaScript From the top frame I want to get the information that the script calling myfunc()
is located in file:///path/to/some/javascript.js. 从顶部框架我想获得调用
myfunc()
的脚本位于file:///path/to/some/javascript.js的信息。
I first expected that we could simply use the IActiveScriptDebug
interface to get a stacktrace from our utility function. 我首先期望我们可以简单地使用
IActiveScriptDebug
接口从我们的实用程序函数中获取堆栈跟踪。 However, it appears to be impossible to get the IActiveScript
interface from an IWebBrowser2
interface or associated document (see Full callstack for multiple frames JS on IE8 ). 但是,似乎无法从
IWebBrowser2
接口或相关文档获取IActiveScript
接口(请参阅IE8上的多帧JS的完全调用 )。
The only thing I can think of is to register our own script debugger implementation and have myfunc()
break into the debugger. 我唯一能想到的是注册我们自己的脚本调试器实现并让
myfunc()
进入调试器。 However, I'm skeptical that this will work without prompting the user about whether they want to break into the debugger. 但是,我怀疑这会在没有提示用户是否想要进入调试器的情况下工作。
Before doing more thorough tests of this approach, I wanted to check whether anyone has definitive information about whether this is likely to work and/or can suggest an alternative approach that will enable a function written in C++ to get a stack trace from the scripting engine that invoked it. 在对这种方法进行更全面的测试之前,我想检查是否有人有关于这是否可行的确切信息和/或可以建议一种替代方法,该方法将使用C ++编写的函数从脚本引擎获取堆栈跟踪调用它。
Each script you load may have an id and each method of the script calling myfunc() may pass this id to myfunc(). 您加载的每个脚本都可能有一个id,调用myfunc()的脚本的每个方法都可以将此id传递给myfunc()。 This means that first you have to modify myfunct() and finally alter your scripts and calls.
这意味着首先必须修改myfunct()并最终修改脚本和调用。
This answer describes how I solved the actual issue I described in the original question. 这个答案描述了我如何解决我在原始问题中描述的实际问题。 The question description isn't great since I was making assumptions about how to solve the problem that actually turned out to be unfounded.
问题描述并不是很好,因为我在假设如何解决实际上没有根据的问题。 What I was really trying to do is determine the path of the currently running script.
我真正想要做的是确定当前运行脚本的路径。 I've changed the title of the question to more accurately reflect this.
我已经改变了问题的标题,以便更准确地反映这一点。
This is actually fairly easy to achieve since scripts are executed in an HTML document as they are loaded. 这实际上相当容易实现,因为脚本在加载时在HTML文档中执行。 So if I am currently executing some JavaScript that is loaded by a
script
tag, that script
tag will always be the last script tag in the document (since the rest of the document hasn't loaded yet). 因此,如果我当前正在执行一些由
script
标记加载的JavaScript,那么该script
标记将始终是文档中的最后一个脚本标记(因为文档的其余部分尚未加载)。 To solve this problem, it is therefore enough just to get the URL of the src
attribute of the last script tag and resolve any relative paths based on that. 因此,要解决此问题,只需获取最后一个脚本标记的
src
属性的URL,并根据该标记解析任何相对路径。
Of course this doesn't work for script embedded directly in the HTML page, but that is bad practice anyway (IMO) so this doesn't seem like a very important limitation. 当然这对于直接嵌入HTML页面的脚本不起作用,但这无论如何都是不好的做法(IMO),所以这似乎不是一个非常重要的限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.