Skip to content

Instantly share code, notes, and snippets.

@austin-dudzik
Created Jul 22, 2021
Embed
What would you like to do?
Companion code for "Earn Microsoft Rewards from any search engine (w/JavaScript)" tutorial
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Loading...</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<script>
// Run when the document is ready
$(document).ready(function () {
// Create a new variable to grab search params
var urlParams = new URLSearchParams(window.location.search);
// Get the specific query
var searchParam = urlParams.get('q');
var searchProvider = urlParams.get('p');
// Create hidden iframe and load Bing search
$("<iframe>").attr({ src: "https://bing.com?q=" + searchParam, id: "bingSearch" }).css("display", "none").appendTo("body");
// Add loading message to page
$("<p>").text("Loading...").appendTo("body");
// Logic to decide which provider to use
switch (searchProvider) {
case "google":
providerUrl = "https://google.com/search?q=";
break;
case "yahoo":
providerUrl = "https://search.yahoo.com/search?p=";
break;
case "baidu":
providerUrl = "http://www.baidu.com/s?wd=";
break;
case "aol":
providerUrl = "https://search.aol.com/aol/search?q=";
break;
case "ask.com":
providerUrl = "https://www.ask.com/web?q=";
break;
case "duckduckgo":
providerUrl = "https://duckduckgo.com/?q=";
break;
case "yandex":
providerUrl = "https://yandex.com/search/?text=";
break;
default:
providerUrl = searchProvider;
}
// Once Bing search has loaded...
$("#bingSearch").on("load", function () {
// Redirect to Google results
window.location.replace(providerUrl + searchParam);
})
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment