Hacker News Search

Add a search box on the top of the page and make it search all of HackerNews.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Hacker News Search
// @namespace   https://news.ycombinator.com/
// @match     https://news.ycombinator.com/*
// @author       Jonathan Woolf
// @description Add a search box on the top of the page and make it search all of HackerNews.
// @version     1
// @grant       none
// ==/UserScript==

// Create the search container
var searchContainer = document.createElement("center");
searchContainer.setAttribute("style", "background-color: #FFFFFF; padding: 10px;");

// Create the search input
var searchInput = document.createElement("input");
searchInput.setAttribute("type", "text");
searchInput.setAttribute("placeholder", "Search Hacker News");
searchInput.setAttribute("style", "padding: 5px;");
searchInput.setAttribute("id", "search-input")

// Create the search button
var searchButton = document.createElement("button");
searchButton.innerHTML = "Search";
searchButton.setAttribute("style", "padding: 5px; margin-left: 10px;");

// Add the search input and button to the container
searchContainer.appendChild(searchInput);
searchContainer.appendChild(searchButton);

// Add the container to the top of the page
document.body.insertBefore(searchContainer, document.body.firstChild);

// Add an event listener to the search button
searchButton.addEventListener("click", function() {
  performSearch();
});

//Add event listener for return key press
document.getElementById("search-input").addEventListener("keyup", function(event) {
  // Number 13 is the "Enter" key on the keyboard
  if (event.keyCode === 13) {
    event.preventDefault();
    performSearch();
  }
});

function performSearch() {
  // Get the search query
  var query = searchInput.value;

  // Build the search URL
  var searchURL = "https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=" + query + "&sort=byPopularity&type=story";

  // Redirect to the search URL
  window.location.href = searchURL;
}