Skip to content

Commit

Permalink
feat: add basic sort
Browse files Browse the repository at this point in the history
  • Loading branch information
hyldmo committed Dec 22, 2022
1 parent a22d44d commit 78f8f02
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
11 changes: 9 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
"48": "images/icon-48.png",
"128": "images/icon-128.png"
},
"action": {
"default_title": "Sort items by score"
},
"content_scripts": [
{
"js": ["index.js"],
"js": ["main.js"],
"matches": ["https://www.vinmonopolet.no/*"]
}
]
],
"permissions": ["activeTab", "scripting"],
"background": {
"service_worker": "background.js"
}
}
2 changes: 1 addition & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { RollupOptions } from 'rollup'
import copy from 'rollup-plugin-copy'

const config: RollupOptions = {
input: 'src/index.ts',
input: ['src/main.ts', 'src/background.ts', 'src/sort.ts'],
output: {
sourcemap: true,
dir: 'dist',
Expand Down
10 changes: 10 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
chrome.action.onClicked.addListener(tab => {
if (tab.id) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['sort.js']
})
} else {
console.error(`No tab id for tab window ${tab.windowId}`, tab)
}
})
1 change: 1 addition & 0 deletions src/index.ts → src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function updateRatingElement(product: Product) {
}

const wines = new Map<string, Product>()
window.wines = wines

async function onProductListChange() {
const items: HTMLElement[] = Array.from(document.querySelectorAll('.product-item'))
Expand Down
17 changes: 17 additions & 0 deletions src/sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function sortElements() {
// wines may already be defined in the global scope / context due how chrome extensions work
const wineList = Array.from(wines?.values() ?? window.wines?.values() ?? []).sort(
(a, b) => (b.stats?.score ?? 0) - (a.stats?.score ?? 0)
)

const listElement = document.querySelector('.product-list')

if (listElement) {
for (const wine of wineList) {
if (wine.element) {
listElement.appendChild(wine.element)
}
}
}
}
sortElements()
8 changes: 8 additions & 0 deletions src/types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Product } from './products'

declare global {
const wines: Map<string, Product> | undefined
export interface Window {
wines?: Map<string, Product> | undefined
}
}

0 comments on commit 78f8f02

Please sign in to comment.