Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
Last commit: 801ef17
  • Loading branch information
JonDerThan committed Feb 15, 2024
1 parent 0116ed8 commit ee0aea5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
80 changes: 45 additions & 35 deletions bookbuyer.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @name BookBuyer
// @author JonDerThan
// @namespace JonDerThan.github.com
// @version 1.2.0
// @version 1.2.1
// @description Allows for quick searching of goodread books on an arbitrary website.
// @match https://www.goodreads.com/*
// @iconURL https://raw.githubusercontent.com/JonDerThan/bookbuyer/main/src/bookbuyer-favicon.png
Expand All @@ -26,7 +26,7 @@ let USE_SEARCH_SITE_FAVICON = false
// Whether to use the DuckDuckGo image proxy for the favicons.
let USE_DDG_PROXY_FAV = false
// Whether to include the author's name in the search.
let inclAuthor = false
let INCL_AUTHOR = false

// You can include search parameters of your search site. If your configured
// search site offers a filter for content choice for example, you'll notice
Expand All @@ -45,51 +45,20 @@ let searchParams = [

// ---------- CONFIGURATION END -----------------------------------------------

const DDG_IMG_PROXY = "https://external-content.duckduckgo.com/iu/?u="
let ICON_URL = "https://raw.githubusercontent.com/JonDerThan/bookbuyer/main/src/bookbuyer-favicon.png"
let SEARCH_HOSTNAME = ""
let pendingChecks = [-1, -1, -1, -1]

// Check whether the script is currently executed as an add-on.
function isAddon() {
return typeof chrome !== "undefined" && typeof chrome.runtime !== "undefined"
}

// Run as an add-on.
if (isAddon()) {
ICON_URL = chrome.runtime.getURL("bookbuyer-favicon.png")
// FIXME: Despite documentation saying otherwise, apparently Firefox supports
// both the callback AND the promises API for the `chrome.storage.sync.get`
// API, when run with manifest v2. As per the docs, with v2, only the
// callback API should be supported. Since the Chrome version is run with v3
// anyways (where the promises API is adopted), this works in both Firefox
// and Chrome. Since the Firefox version should switch over to v3 soon, this
// undocumented version is kept. Remove this note when manifest v3 is used
// for the Firefox version too.
// Note: Seemingly this only applies to content scripts. In the settings page
// only the callback API is supported.
chrome.storage.sync.get("settings")
.then(data => {
parseSettings(data)
main()
})
.catch(e => {
console.error("error loading settings: " + e)
main()
})
}

// Run as a userscript.
else {
main()
}

function parseSettings(data) {
if (!Object.prototype.hasOwnProperty.call(data, "settings")) return
const settings = data.settings

// Parse the add-on settings.
inclAuthor = settings.findIndex(s => s[0] === "incl_author") != -1
INCL_AUTHOR = settings.findIndex(s => s[0] === "incl_author") != -1
USE_SEARCH_SITE_FAVICON = settings
.findIndex(s => s[0] === "use_search_site_fav") != -1
USE_DDG_PROXY_FAV = settings
Expand Down Expand Up @@ -123,12 +92,14 @@ function getURL(search, author) {
.map(s => encodeURIComponent(s[0]) + "=" + encodeURIComponent(s[1]))
.join("&")
if (httpGetList.length > 0) httpGetList += "&"
if (inclAuthor && author) search += " " + author
if (INCL_AUTHOR && author) search += " " + fmtAuthor(author)
httpGetList += SEARCH_PARAM + "=" + encodeURIComponent(search)
return `${SEARCH_SITE}?${httpGetList}`
}

function getIconURL() {
const DDG_IMG_PROXY = "https://external-content.duckduckgo.com/iu/?u="

if (!USE_SEARCH_SITE_FAVICON)
return ICON_URL

Expand Down Expand Up @@ -211,6 +182,15 @@ function getTitle(elem) {
return matches[1]
}

// Author's name may be in a format "LASTNAME, FIRSTNAME". This function always
// returns "FIRSTNAME LASTNAME".
function fmtAuthor(author) {
if (!author.includes(","))
return author

return author.split(",").reverse().join(" ").trim()
}

function createLink(title, author) {
// create img
let img = document.createElement("img")
Expand All @@ -231,6 +211,7 @@ function createLink(title, author) {
return a
}

let pendingChecks = [-1, -1, -1, -1]
function refreshPendingChecks(func) {
// clear out remaining ones:
pendingChecks.forEach(clearTimeout)
Expand Down Expand Up @@ -274,3 +255,32 @@ async function main() {
injectLinks()
}

// Run as an add-on.
if (isAddon()) {
ICON_URL = chrome.runtime.getURL("bookbuyer-favicon.png")
// FIXME: Despite documentation saying otherwise, apparently Firefox supports
// both the callback AND the promises API for the `chrome.storage.sync.get`
// API, when run with manifest v2. As per the docs, with v2, only the
// callback API should be supported. Since the Chrome version is run with v3
// anyways (where the promises API is adopted), this works in both Firefox
// and Chrome. Since the Firefox version should switch over to v3 soon, this
// undocumented version is kept. Remove this note when manifest v3 is used
// for the Firefox version too.
// Note: Seemingly this only applies to content scripts. In the settings page
// only the callback API is supported.
chrome.storage.sync.get("settings")
.then(data => {
parseSettings(data)
main()
})
.catch(e => {
console.error("error loading settings: " + e)
main()
})
}

// Run as a userscript.
else {
main()
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bookbuyer",
"version": "1.2.0",
"version": "1.2.1",
"description": "Allows for quick searching of goodread books on an arbitrary website.",
"devDependencies": {
"eslint": "^8.34.0",
Expand Down
2 changes: 1 addition & 1 deletion src/bookbuyer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @name BookBuyer
// @author JonDerThan
// @namespace JonDerThan.github.com
// @version 1.2.0
// @version 1.2.1
// @description Allows for quick searching of goodread books on an arbitrary website.
// @match https://www.goodreads.com/*
// @iconURL https://raw.githubusercontent.com/JonDerThan/bookbuyer/main/src/bookbuyer-favicon.png
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "BookBuyer",
"author": "JonDerThan",
"version": "1.2.0",
"version": "1.2.1",
"description": "Allows for quick searching of goodread books on an arbitrary website.",
"homepage_url": "https://github.com/JonDerThan/bookbuyer",

Expand Down
2 changes: 1 addition & 1 deletion src/manifest_v3.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"name": "BookBuyer",
"author": "JonDerThan",
"version": "1.2.0",
"version": "1.2.1",
"description": "Allows for quick searching of goodread books on an arbitrary website.",
"homepage_url": "https://github.com/JonDerThan/bookbuyer",

Expand Down

0 comments on commit ee0aea5

Please sign in to comment.