首页 > 解决方案 > Get and search for the hovered link URL "only"

问题描述

As the title suggests

「 Get and search for the hovered link URL "only" 」</p>

What corrections would be needed to achieve this?

// Pattern1⃣ Malfunction
// ① Opens all URLs that contain the specified word, regardless of mouse over target.
// ② Opens with or without mouseover.
var ele = document.querySelectorAll(".centerarticle-entry-title a");
var link = ['Loading...', 'Dance Party'];
var L = window.onload = function(load) {
    window.addEventListener('keydown', (e) => {
        if (e.ctrlKey && e.keyCode === 13) { // CTRL + ENTER
            for (let i = 0; i < ele.length; i++) {
                for (let j = 0; j < link.length; j++) {
                    if (!link.onmouseenter) {
                        e.stopPropagation();
                    }
                    if (ele[i].innerHTML.match(link[j])) {
                        ele[i].innerHTML.match(link[j]).onmouseenter = window.open("https://web.archive.org/web/" + ele[i].href);
                        L = undefined;
                        ele[i].onmouseenter = undefined;
                        ele[i].onmouseenter = null;
                    }
                }
            }
        } else {
            ele[i].onmouseleave = e.preventDefault();
            return false;
        }
    });
};

In the case of Pattern1⃣, this is practical and convenient in another sense, but the function we want to implement this time is different from Pattern1⃣.

The function you want to implement is 「 Get and search for the hovered link URL "only" 」 I am particular about it.

I've created a few other prototypes, but they're even less practical because the version of Pattern 1⃣ is even worse or the search results are empty.

// Pattern2⃣ Malfunction
// ① Opens all URLs that contain the specified word, regardless of mouse over target.
// ② There is a 'case' that opens even if you don't mouse over.
// ③ In some cases, nothing responds in the first place, in which case you need to do a super reload etc. each time.
// ④ The number of times you pressed the ENTER key to the sky may have accumulated. Alternatively, the number of hovering times can be accumulated as it is. That possibility can open duplicate TABs unnecessarily.
var ele = document.querySelectorAll(".centerarticle-entry-title a");
var link = ['Loading...', 'Dance Party'];
document.addEventListener('mouseenter', (m_enter) => {
    document.addEventListener('mouseleave', (m_leave) => {
        m_enter.preventDefault();
        e.preventDefault();
        return false;
    });
    window.addEventListener('keydown', (e) => {
        if (!(e.ctrlKey && e.keyCode == 13)) {
            m_enter.preventDefault();
            return false;
        }
        if (e.ctrlKey && e.keyCode == 13) {
            for (let i = 0; i < ele.length; i++) {
                for (let j = 0; j < link.length; j++) {
                    if (ele.innerHTML.match(link[j])) {
                        link[j].onmouseover = window.open("https://web.archive.org/web/" + ele[i].href);
                        location.reload();
                        break;
                    }
                }
            }
        } else {
            return false;
        }
    });
});


// Pattern3⃣ Malfunction
// ① Opens only one (probably top) URL that contains the specified word, regardless of whether you hover your mouse over the target.
// ② Opens with or without mouseover.
var ele = document.querySelectorAll(".centerarticle-entry-title a");
var link = ['Loading...', 'Dance Party'];
window.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.keyCode === 13) { // CTRL + ENTER key
        for (let i = 0; i < ele.length; i++) {
            for (let j = 0; j < link.length; j++) {
                if (ele[i].innerHTML.match(link[j])) {
                    link[j].onmouseover = window.open(("https://web.archive.org/web/" + ele[i].href));
                    return false;
                }
            }
        }
    }
});


// Pattern4⃣ Malfunction
// ① Opens with or without mouseover.
// ② Search results are empty or "undefined"
var ele = document.querySelectorAll(":hover");
var link = ['Loading...', 'Dance Party'];
window.addEventListener('keydown', (e) => {
    if (e.ctrlKey && e.keyCode == 13) {
        link.onmouseenter =
            window.open("https://web.archive.org/web/" + this.href);
        return false;
    }
});

The actual experiment target pages are as follows.

https://b.hatena.ne.jp/search/tag?q=bookmarklet&page=67

please tell me,

标签: javascriptjquerymouseeventmouseovermouselistener

解决方案


<a href="https://google.com">Google</a>
<a href="https://yahoo.com">Yahoo</a>
<script>
document.onkeydown = () => {
  const link = document.querySelector('a:hover')
  if (link) window.open(link.href)
}
</script>


推荐阅读