GitHub Gist Search Box

Show search box in gist's userpage

Verze ze dne 17. 01. 2020. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name GitHub Gist Search Box
  3. // @namespace me.nzws.us.gist_box
  4. // @version 1.0.0
  5. // @description Show search box in gist's userpage
  6. // @author nzws
  7. // @match https://gist.github.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. const html = `
  12. <div class="mb-3 mb-md-0 mr-md-3 flex-auto">
  13. <input type="search" class="form-control width-full" placeholder="Find a public gist…" />
  14. </div>
  15.  
  16. <div class="d-flex">
  17. <button class="text-center btn btn-primary ml-3">
  18. Search
  19. </button>
  20. </div>
  21. `;
  22. const $ = e => document.querySelector(e);
  23.  
  24. const onSearch = () => {
  25. const username = window.location.pathname.slice(1);
  26. const input = `user:${username} ${$('#gist-search-box input').value}`;
  27. location.href = `/search?q=${encodeURIComponent(input)}`;
  28. };
  29.  
  30. (function() {
  31. 'use strict';
  32.  
  33. const checkUserPage = $('.reponav-item.selected[aria-label="All gists"]');
  34. if (!checkUserPage) return;
  35.  
  36. const newNode = document.createElement('div');
  37. newNode.className = 'd-block d-md-flex mb-4';
  38. newNode.id = 'gist-search-box';
  39. newNode.innerHTML = html;
  40.  
  41. const pagehead = $('.pagehead');
  42. pagehead.parentNode.insertBefore(newNode, pagehead);
  43.  
  44. $('#gist-search-box button').onclick = onSearch;
  45. $('#gist-search-box input').onkeypress = e => e.keyCode === 13 ? onSearch() : true;
  46. })();