双击搜索

Search on double click

我试图通过双击某个词来触发对 Mediawiki 站点的搜索,但它不起作用。这是在 Common.js.

中添加的内容
document.addEventListener("dblclick", function () {
    var w = String(window.getSelection());
    if (w) browser.runtime.sendMessage(null, { searchFor: w, dblClick: true });
});


function doSearch(word) {
    if (!word) return;
    word = word.replace(/[\r\n ]+/g, " ").trim();
    if (!word || word.length > 70) return;
    browser.tabs.create({
        "url": "https://lsj.gr/index.php?search=" + encodeURIComponent(word)
    });
}

我没有看到调用了 doSearch。如果您将它添加到您的事件侦听器,那么事情可能会如您所愿:

document.addEventListener("dblclick", function () {
    var w = String(window.getSelection());
    if (w){
      browser.runtime.sendMessage(null, { searchFor: w, dblClick: true });
      doSearch(w)
    }
});

也许试试window.open()

工作示例

document.addEventListener("dblclick", function () {
    var w = String(window.getSelection());
    if (w){
       window.open("https://www.mediawiki.org/w/index.php?search=" + encodeURIComponent(w) )
    }
});