简体   繁体   English

如何使用Javascript获取当前运行脚本的路径?

[英]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
  • file:///path/to/some/javascript.js 文件:///path/to/some/javascript.js
  • (additional stack frames) (附加堆栈帧)

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.

相关问题 如何获取JavaScript当前运行的线路代码? - How to get the code of the line currently running in JavaScript? 我可以在 JavaScript 中获取当前正在运行的函数的名称吗? - Can I get the name of the currently running function in JavaScript? 如何获取当前正在执行的javascript代码的文件路径 - How to get the file-path of the currently executing javascript code 如何中断当前运行的JavaScript - How to break on currently running JavaScript JavaScript-如何停止运行onClick()的脚本? - JavaScript - How do I stop a script from running onClick()? 如何从脚本路径获取查询字符串值? - How do I get query string value from script path? 如何获取当前打开的excel工作簿的本地路径? - How can I get the local path of the currently open excel workbook? 如何通过JavaScript获取<select>中当前选择的<option>? - How do you get the currently selected <option> in a <select> via JavaScript? JavaScript for Automation - 如何获取当前脚本的路径? - JavaScript for Automation - how to get the path of the current script? 在 firebase (javascript) 中,如何获取当前登录用户的数据? - In firebase (javascript), how can I get data for the currently logged in user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM