1

I am trying to build a chrome extension that takes a highlighted text (the use case is going to be books), searches for that text on Amazon, and auto-populates a corresponding title, book cover, and its author on the Chrome Extension popup window.

So far, I have been successful in passing the highlighted variable from the script file to the popup.js file. The question is this: is it possible for me to request a search result page, then run the typical querySelector methods to create an array of the necessary information, all without opening the window itself?

var searchLink = 'https://www.amazon.com/s?k=' + highlightedText 
fetch(searchLink){
 // target a certain class/id and return an array of information 

Very new to JS, so apologies if this is a foolish question. I am getting a cross-origin denial when I try this in my browser console. Is this something that requires an Amazon affiliate/developer API? Thanks in advance.

Kiwon Yun
  • 43
  • 6
  • provided you're not getting blocked due to cross-origin denial, start with `fetch("https://......").then(result => result.blob()).then(blob => blob.text()).then(text => console.log(text)).catch(e => console.error(e));`. With that page source, I'm sure you can now use google etc. to figure out how to turn that into a separate document that you can run query selectors on. – Mike 'Pomax' Kamermans Jan 15 '20 at 04:40
  • 1
    `fetch` followed by `DOMParser` looks like it should do the trick – CertainPerformance Jan 15 '20 at 04:42
  • @Mike'Pomax'Kamermans Edited the question -- didn't include that I was indeed getting a cross-origin denial. – Kiwon Yun Jan 15 '20 at 04:45
  • 1
    @Mike'Pomax'Kamermans `.then(result => result.blob()).then(blob => blob.text())` really? Why don't you consume that response as text directly? – Kaiido Jan 15 '20 at 04:46
  • @Kiwon Yun It sounds like you're looking for Product Advertising API for Amazon. There's some hurdles to jump over to get access to it. The Product Advertising API is available to Associates that have been reviewed and received final acceptance into the Associates Program. The first step is for you to make three qualifying sales through your links. After this is done your account will be reviewed by a specialist. Have you considered using Google Books API? – peyo Jan 15 '20 at 04:46
  • @peyo Thanks, I was in the process of applying for one anyways (the three sales hurdles are a bit annoying for starting out). I have considered Google Books, but have found that Amazon search results, cover image quality, and general consistency tends to be a bit better... – Kiwon Yun Jan 15 '20 at 04:48
  • @Kaiido because ideally they'd have discovered that themselves while looking up those constructions, which is a more potent form of learning. (which is why that was a comment, not an answer) – Mike 'Pomax' Kamermans Jan 15 '20 at 04:49

0 Answers0