0
0

今開いているサイトでサイト内検索するためのブックマークレット

Last updated at Posted at 2023-10-31

ソースコード

これをブックマークに登録

javascript: (function () {
    let frm = document.createElement("form");
    frm.style.position = "fixed";
    frm.style.top = "10px";
    frm.style.left = "10px";
    frm.style.zIndex = 999999;
    frm.style.width = "300px";
    frm.style.height = "20px";
    frm.action = "https://www.google.co.jp/search";
    let q = document.createElement("input");
    q.type = "text";
    q.name = "q";
    q.style.width = "200px";
    q.onkeydown = function(e){
        if(e.key === 'Escape'){
            frm.parentNode.removeChild(frm);
        }
    };
    frm.appendChild(q);
    let hid = document.createElement("input");
    hid.type = "hidden";
    hid.name = "as_sitesearch";
    hid.value = window.location.hostname;
    frm.appendChild(hid);
    let sub = document.createElement("input");
    sub.type = "submit";
    sub.value = "検索";
    frm.appendChild(sub);
    document.body.appendChild(frm);
    q.focus();
  })();

実行結果

image.png

image.png

解説

  • サイト内検索のためのGoogle検索パラメータを埋め込んだフォームを今開いているbodyにappendChildしているだけ
  • エスケープキーで消えるとかはこの手のPOPUPでありがち
  • スタイルの調整はあまりしてないので、今開いてるページのスタイルに引っ張られるけど、特に不便はないので、最低限のスタイル調整
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0