JavaScript记录通过JavaScript按钮点击打开的新标签页的URL。

huangapple go评论58阅读模式
英文:

Javascript log the url of the new tab opened from a javascript click of a button

问题

// 寻找包含“View”按钮的td元素
const tdElement = document.querySelector('td[style="cursor: pointer; color: blue;"]');

// 点击“View”按钮
if (tdElement) {
  tdElement.click();

  // 监听新窗口的加载事件
  window.addEventListener('load', () => {
    console.log('新窗口已打开,URL为:', window.location.href);

    // 关闭新窗口
    window.close();
  });
} else {
  console.log('找不到“View”按钮');
}
英文:

I have a javascript code that clicks and opens a link in a new tab. I have to click the button in order to find the url because when you inspect the element, it doesn't contain any value for the href attribute. But when you click, the link opens a new tab. So I need to console.log the link which has been opened and then close it.

Important to mention that I am running the code inside a browser developer console.

Here is my best attempt:

// Find the td element containing the "View" button
const tdElement = document.querySelector('td[style="cursor: pointer; color: blue;"]');

// Click the "View" button
if (tdElement) {
  tdElement.click();

  // Listen for the load event on the new window
  window.addEventListener('load', () => {
    console.log('New window opened with URL:', window.location.href);

    // Close
    window.close();
  });
} else {
  console.log('Could not find "View" button');
}

UPDATED

// Find the td element containing the "View" button
const tdElement = document.querySelector('td[style="cursor: pointer; color: blue;"]');

// Click the "View" button
if (tdElement) {
  tdElement.click();

  // Listen to the newly opened tab
  window.addEventListener('focus', () => {
    const openedUrl = window.location.href;
    console.log('Opened URL:', openedUrl);

    // Close the newly opened tab
    window.close();
  }, { once: true });
} else {
  console.log('Could not find "View" button');
}

Please help.

答案1

得分: 1

在这种情况下,如果新窗口通过Javascript打开并且使用window.open,你可以说是劫持window.open

新函数将url存储在一个变量中:

let tempUrl = '';
  
window.open = function(url) {
  tempUrl = url;
}

// 找到包含“View”按钮的td元素
const tdElement = document.querySelector('td[style="cursor: pointer; color: blue;"]');

// 点击“View”按钮
if (tdElement) {
  tdElement.click();
  
  console.log(tempUrl);

} else {
  console.log('找不到“View”按钮');
}

这样,如果你只需要url,也可以避免再次关闭打开的窗口。

英文:

In case the new window opens with Javascript and window.open is used for it, you can hijack window.open so to speak.

The new function stores the url in a variable:

let tempUrl = '';

window.open = function(url) {
  tempUrl = url;
}

// Find the td element containing the "View" button
const tdElement = document.querySelector('td[style="cursor: pointer; color: blue;"]');

// Click the "View" button
if (tdElement) {
  tdElement.click();
  
  console.log(tempUrl);

} else {
  console.log('Could not find "View" button');
}

This way you also save closing the open window again, if you only need the url.

huangapple
  • 本文由 发表于 2023年3月9日 19:13:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定