Last active
September 24, 2024 20:07
-
-
Save AJolly/7791ebddb4bafcd78105a446f440d900 to your computer and use it in GitHub Desktop.
Amazon Easy Review Search Userscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Amazon Review Search | |
// @namespace https://gist.github.com/AJolly | |
// @version 0.6 | |
// @description Add a search box for customer reviews on Amazon product pages | |
// @match https://www.amazon.com/* | |
// @grant none | |
// @author AJolly | |
// @license MIT | |
// @downloadURL https://gist.github.com/AJolly/7791ebddb4bafcd78105a446f440d900/raw/?cache-bust=111 | |
// @updateURL https://gist.github.com/AJolly/7791ebddb4bafcd78105a446f440d900/raw/?cache-bust=111 | |
// ==/UserScript== | |
// 0.6 - Changed to use match rather than include, and allowed using the keyboard to trigger the search. | |
(function() { | |
'use strict'; | |
// Function to extract ASIN from the current URL | |
function getASIN() { | |
const match = window.location.pathname.match(/\/(?:dp|product|gp\/product)\/([A-Z0-9]{10})/); | |
return match ? match[1] : null; | |
} | |
// Function to check if we're on a product page | |
function isProductPage() { | |
return !!getASIN(); | |
} | |
// Function to create and insert the search box | |
function addReviewSearchBox() { | |
if (!isProductPage()) return; | |
const askQuestionBox = document.querySelector('#askQuestionTextInput, #askATFLink'); | |
if (!askQuestionBox) return; | |
const existingSearchBox = document.getElementById('ajolly-review-search'); | |
if (existingSearchBox) return; | |
const searchBox = document.createElement('input'); | |
searchBox.type = 'text'; | |
searchBox.id = 'ajolly-review-search'; | |
searchBox.placeholder = 'Search customer reviews'; | |
searchBox.style.marginTop = '10px'; | |
searchBox.style.width = '100%'; | |
searchBox.style.padding = '5px'; | |
const searchButton = document.createElement('button'); | |
searchButton.textContent = 'Search Reviews'; | |
searchButton.style.marginTop = '5px'; | |
searchButton.style.width = '100%'; | |
searchButton.style.padding = '5px'; | |
searchButton.addEventListener('click', function() { | |
const asin = getASIN(); | |
if (!asin) { | |
alert('Unable to find product ASIN. Please make sure you\'re on a product page.'); | |
return; | |
} | |
const keyword = encodeURIComponent(searchBox.value.trim()); | |
const url = `https://www.amazon.com/product-reviews/${asin}/ref=cm_cr_arp_d_viewopt_sr?ie=UTF8&filterByStar=all_stars&reviewerType=all_reviews&pageNumber=1&filterByKeyword=${keyword}#reviews-filter-bar`; | |
window.open(url, '_blank'); | |
}); | |
// Minimal change: Add keyboard support for Enter and Space keys | |
searchButton.addEventListener('keydown', function(event) { | |
if (event.key === 'Enter' || event.key === ' ') { | |
event.preventDefault(); | |
searchButton.click(); | |
} | |
}); | |
const container = document.createElement('div'); | |
container.style.marginTop = '10px'; | |
container.appendChild(searchBox); | |
container.appendChild(searchButton); | |
askQuestionBox.parentNode.insertBefore(container, askQuestionBox.nextSibling); | |
} | |
// Run the script when the page is fully loaded | |
window.addEventListener('load', addReviewSearchBox); | |
// Also run the script on URL changes (for single-page application behavior) | |
let lastUrl = location.href; | |
new MutationObserver(() => { | |
const url = location.href; | |
if (url !== lastUrl) { | |
lastUrl = url; | |
addReviewSearchBox(); | |
} | |
}).observe(document, {subtree: true, childList: true}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment