GM_SelectToSearch

搜索选中文本

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.greasyfork.org/scripts/555706/1694621/GM_SelectToSearch.js을(를) 사용하여 포함하는 라이브러리입니다.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

(function () {
  let selectedText;

  function initButtons(buttons) {
    const $searchBar = document.createElement("div");
    $searchBar.className = `gm-select-to-search`;
    $searchBar.addEventListener("mouseup", (e) => e.stopPropagation());
    document.body.appendChild($searchBar);

    buttons.forEach(({ label, url, prepend = "", append = "" }) => {
      const $button = createButton(label, () => {
        const text = `${prepend} ${selectedText} ${append}`.trim();
        window.open(url.replace("%s", text));
      });
      $searchBar.appendChild($button);
    });
    return $searchBar;
  }

  function createButton(label, onclick) {
    const $button = document.createElement("button");
    $button.innerHTML = label;
    if (onclick) $button.addEventListener("click", onclick);
    return $button;
  }

  function initListener($searchBar) {
    window.addEventListener("mouseup", function onMouseup(e) {
      selectedText = document.getSelection().toString().trim();
      if (selectedText) {
        $searchBar.style.display = "flex";
        const left = Math.min(e.x, window.innerWidth - $searchBar.offsetWidth - 20);
        const top = Math.min(e.y + 20, window.innerHeight - $searchBar.offsetHeight);
        $searchBar.style.left = left + "px";
        $searchBar.style.top = top + "px";
      } else {
        $searchBar.style.display = "none";
      }
    });
  }

  function initStyle({ background = "#edebdf", border = "1px solid #5c0d12" }) {
    const style = document.createElement("style");
    style.innerHTML = `
      .gm-select-to-search {
        display: none;
        position: fixed;
        z-index: 10000;
        padding: 5px;
        background: ${background};
        border: ${border};
  
        & button {
          margin: 2px;
          padding: 0 4px;
          white-space: nowrap;
          cursor: pointer;
          display: flex;
          justify-content: center;
          align-items: center;
  
          & img {
            width: 16px;
            height: 16px;
            margin-right: 2px;
          }
        }
      }
    `;
    document.head.appendChild(style);
  }

  /**
    window.GM_SelectToSearch(
      [
        {
          // 按钮文本
          label: `<img src="https://www.voidtools.com/favicon.ico"> ES`,
          // 搜索链接,%s会被替换为选中文本
          url: "es:%s",
          // (可选)选中文本前面拼接
          prepend: "",
          // (可选)选中文本后面拼接
          append: "",
        },
      ],
      // (可选)搜索栏样式
      {
        background = "#edebdf",
        border = "1px solid #5c0d12",
      }
    );
   */
  window.GM_SelectToSearch = function SelectToSearch(buttons, styles = {}) {
    initStyle(styles);
    const $searchBar = initButtons(buttons);
    initListener($searchBar);
  };
})();