Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取从当前选项卡启动的选项卡URL的JavaScript bookmarklet代码_Javascript_Url_Tabs_Bookmarklet - Fatal编程技术网

获取从当前选项卡启动的选项卡URL的JavaScript bookmarklet代码

获取从当前选项卡启动的选项卡URL的JavaScript bookmarklet代码,javascript,url,tabs,bookmarklet,Javascript,Url,Tabs,Bookmarklet,我正在尝试开发一个bookmarklet。bookmarklet的用途是显示当前选项卡和由于窗口而启动的新选项卡的URL。打开调用。为了简化事情,假设launcher.com启动了一个随机的URL来访问 function getBothURLs() { var currentURL, newWin; function launchNew () { currentURL = window.location.href; newWin =

我正在尝试开发一个bookmarklet。bookmarklet的用途是显示当前选项卡和由于
窗口而启动的新选项卡的URL。打开
调用。为了简化事情,假设
launcher.com
启动了一个随机的URL来访问

function getBothURLs() { 
    var currentURL, newWin;

    function launchNew () {
        currentURL = window.location.href; 
        newWin     = window.open("https://www.launcher.com");
    }

    launchNew();
    alert(currentURL); 
    alert(newWin.location.href); // displays 'about:blank'
}
我无法获取新启动选项卡的URL。
警报();相反,它显示

 about:blank
当我在Chrome控制台中对此进行故障排除时,我将
var currentURL,newWin
的定义移到了
gettourls()
函数的范围之外。当我在控制台中调用函数
getBothURLs()
时,
currentURL
newWin
都有有效数据

如何修改函数
getBothURLs()
以达到预期目的?

MDN中的注释:

…远程URL不会立即加载。返回window.open()时,窗口始终包含about:blank。URL的实际抓取是延迟的,并且在当前脚本块完成执行后开始

在上述情况下,当前块以
getBothURLs()
函数结束,但您尝试检查该块本身中的新URL,根据上面的引文,您会看到
about:blank
URL。
要解决这个问题,只需将查找推迟到下一个任务。您可以使用类似于
setTimeout
的方法将查找发布到下一个任务,或者,如果您希望从函数返回值,则可以使用承诺

示例实现可能如下所示:

function getBothURLs() {
  return new Promise((resolve) => {
    var newWin = window.open("//www.stackoverflow.com");
    setTimeout(() => resolve(newWin))
  }).then((win) => {
    return new Promise((resolve, reject) => {
       var check = setInterval(() => {
         try { 
           if(win.location.href !== "about:blank"){
               clearInterval(check);
               resolve(win.location.href)
           }
         } catch(e) {
           clearInterval(check);
           reject(e);
         }
       }, 50)
     })
   })
}

getBothURLs().then(url => console.log(url))
尽管上述解决方案可行,但我仍然建议您将新窗口的URL存储在一个变量中,然后使用该变量调用
open

更新1: 此示例代码段使用非承诺回调版本,可以返回两个URL。尽管可以修改promise版本以返回两个URL,但由于OP在注释中要求使用非promise版本,所以我提供了新的代码段。 看看:

function getBothURLs(callback){
  var current = window.location.href;
  var win = window.open("https://stackoverflow.com");
  var check =  setInterval(() => {
    try {
      if(win.location.href !== "about:blank"){
        clearInterval(check);
        callback(null, current, win.location.href)
      }
    } catch(e) {
      clearInterval(check);
      callback(e);
    }
  }, 50);
}

getBothURLs((err, _cur, _new) => {
  if(err){
    // some error occurred, probably a cross-origin access
    console.error("Failed to get URL", err);
    return;
  }

  console.log(`Current: ${_cur} New: ${_new}`);
})

最好将随机url值添加到一个变量中,如:newUrl=”“,然后将该值分配给newWin变量,如:newWin=window.open(newUrl);完成此操作后,请尝试放置以下行:alert(newUrl);我希望它能起作用。我假设您正在另一个来源上打开url。您有权访问其他url的代码库吗@AntonySUTHAKARJ的评论很有道理,你能解释一下为什么这对你不起作用吗?出于所有实际目的,URL是在同一个来源上打开的。但由于URL将包含一个自动生成的十六进制数,因此无法提前知道。是否有理由不将
urlToOpen
作为参数传递给
getBothURLs
?我认为这里应该有一个带有输入和输出的函数。当有交叉源链接时,这个代码进入一个连续循环。其次,它不会同时返回两个URL(它只返回新的URL)。而且,很高兴知道Promise能做什么,但这不是一种过度的杀伤力吗?@lifebalance好主意!我已经修复了代码片段以处理错误检查并防止无限循环,但我想指出您之前在OP上提到的一条评论-“.出于所有实际目的,URL是在同一个源上打开的”-因此我没有在早期版本中包括这一点,尽管是nvm,总是需要进行一点错误检查good@lifebalance就Promise而言,我认为我的解决方案使用异步方法来检查url(setInterval和all),因此您需要某种方法在完成后通知调用方,因此我在这里使用Promise。。。如果你愿意,你也可以选择回拨…谢谢你的修复。如何使函数返回两个URL?如果不太麻烦的话,还可以了解非承诺回调版本。