Hacker News Search

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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;
}