Get page URL in IE context menu extension

2019-07-24 23:16发布

问题:

I am using IE as my browser, what I want to do is : 1. to do right click in IE page, then click a custom menu which call an external script in my local. 2. this external script is used to get the current page url where I do this right click action.

Supposed I open https://docs.microsoft.com/en-us/sql/t-sql/statements/drop-external-resource-pool-transact-sql in my IE browser, then I do right click to execute my external script from my local. So what I want to get is the exact URL (https://docs.microsoft.com/en-us/sql/t-sql/statements/drop-external-resource-pool-transact-sql)

I have done the 1st step. But I can't find a way to do the 2nd step. I wonder if it is possible to do? I imagine to use window.location.href; in my external script in my local. But the location that comes out is location of the file in my local PC, not the URL from browser. Please help if you know how to do this!

回答1:

Make sure you followed the steps which I shared about adding context menu to IE. Then it's enough to use the following script as content of your html file to get url of the page.

You can get the url from document object of the window:

<script type="text/javascript">
    var parentwin = external.menuArguments;
    var url = parentwin.document.URL;
    alert(url)
</script>