-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
44 lines (38 loc) · 1.74 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
function getISBN13fromLink(){
var link = window.location.href;
var delimeter = "/dp/";
if (link.indexOf('dp/') === -1){
delimeter = "/product/";
}
return link.slice(link.indexOf(delimeter)+delimeter.length, link.indexOf(delimeter) + delimeter.length + 10) // 10 is ISBN10 length
}
function makeGoodreadsLink(isbn13){
return "https://www.goodreads.com/search?q="+isbn13;
}
function addGoodreadsBtn(){
var goodreads_link = makeGoodreadsLink( getISBN13fromLink() );
var avg_custom_reviews = document.querySelector('#averageCustomerReviews');
if (avg_custom_reviews !== null){
avg_custom_reviews.innerHTML += `<br><br><a href="${goodreads_link}">See book on <img src="${chrome.runtime.getURL('goodreads_logo.svg')}" height="20px"/></a><br><br>`;
return;
}
var book_formats = document.querySelector('#formats');
if (book_formats !== null){
book_formats.innerHTML += `<br><br><a href="${goodreads_link}">See book on <img src="${chrome.runtime.getURL('goodreads_logo.svg')}" height="20px"/></a><br><br>`;
return;
}
}
var is_book_check_1 = document.body.innerHTML.indexOf('ISBN-10') !== -1;
if(is_book_check_1){
addGoodreadsBtn();
}else{
var is_book_check_2 = getElementByXpath('//*[@id="nav-subnav"]/a[1]/span') !== null && getElementByXpath('//*[@id="nav-subnav"]/a[1]/span').innerText === "Books";
var is_book_check_3 = document.querySelector('#nav-subnav[data-category=books]') !== null;
var is_book_check_4 = document.querySelector('#nav-subnav[data-category=digital-text]') !== null;
if(is_book_check_2 || is_book_check_3 || is_book_check_4){
addGoodreadsBtn();
}
}