Skip to content
Merged

Dev #19

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
"@babel/preset-react": "^7.27.1",
"@babel/preset-typescript": "^7.27.1",
"@types/chrome": "^0.1.38",
"@types/diff": "^8.0.0",
"@types/html-to-text": "^9.0.4",
"babel-loader": "^10.0.0",
"copy-webpack-plugin": "^13.0.0",
"css-loader": "^7.1.2",
"diff": "^9.0.0",
"fast-xml-parser": "^5.2.3",
"fuse.js": "^7.1.0",
"html-to-text": "^9.0.5",
Expand Down
22 changes: 11 additions & 11 deletions src/background/events/injects/display_history.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { Module } from "../../../types/module";
import { Module } from "../../../types/module"

export default <Module> {
export default <Module>{
setting: s => {
return !!s.recents_list_module
},
condition: async (base, settings, helper_fns) => {
return (await helper_fns.url_begins_with("/news"))
return await helper_fns.url_begins_with("/news")
},
action: async (base, settings, helper_fns) => {
await chrome.scripting.executeScript({
target: {tabId: base.tab_id},
files: ["display_history.js"]
target: { tabId: base.tab_id },
files: ["display_history.js"],
})

await chrome.scripting.insertCSS({
target: {tabId: base.tab_id},
files: ["news_history.css"]
target: { tabId: base.tab_id },
files: ["news_history.css"],
})

if (settings.schooltape_compatibility) {
await chrome.scripting.insertCSS({
target: {tabId: base.tab_id},
files: ["schooltape/post_history_styles.css"]
target: { tabId: base.tab_id },
files: ["schooltape/post_history_styles.css"],
})
}
}
}
},
}
10 changes: 4 additions & 6 deletions src/background/events/injects/news_tracker.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Module } from "../../../types/module"

import { poll_feed } from "../../pull_feed"

export default <Module>{
setting: s => {
return !!s.record_post_history
},
action: async (base) => {
await chrome.scripting.executeScript({
target: { tabId: base.tab_id },
files: ["history_puller.js"],
})
action: async () => {
await poll_feed()
},
}
16 changes: 16 additions & 0 deletions src/background/events/injects/reduce_width.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module } from "../../../types/module"

export default <Module>{
setting: s => {
return s.reduce_content_width
},
condition: (_base, _settings, helper_fns) => {
return helper_fns.is_schoolbox_page
},
action: async base => {
await chrome.scripting.insertCSS({
target: { tabId: base.tab_id },
files: ["reduce_width.css"],
})
},
}
11 changes: 9 additions & 2 deletions src/background/events/on_update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { check_in_schoolbox_domain, get_stored_settings, is_page, url_begins_with } from "../functions/utls/is_page"
import {
check_in_schoolbox_domain,
get_stored_settings,
is_page,
url_begins_with,
} from "../functions/utls/is_page"
import track_page_if_in_domain from "../functions/utls/track_page"

import dark_theme_css from "./injects/dark_theme_css"
Expand All @@ -9,6 +14,7 @@ import launcher_shortcut from "./injects/launcher_shortcut"
import news_tracker_fetch from "./injects/news_tracker"
import news_tracker_display from "./injects/display_history"
import detect_schooltape from "./injects/st_detect_inject"
import reduce_width from "./injects/reduce_width"

const INJECTS = [
dark_theme_css,
Expand All @@ -18,7 +24,8 @@ const INJECTS = [
launcher_shortcut,
news_tracker_fetch,
news_tracker_display,
detect_schooltape
detect_schooltape,
reduce_width,
]

export default async function on_update(tab_id: number, _: any, tab: chrome.tabs.Tab) {
Expand Down
89 changes: 14 additions & 75 deletions src/background/functions/calculate_rev_metrics.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,24 @@
import { RevisionData } from "../../types/rev_history"
import { diffChars } from "diff"

export async function calculate_new_metrics(old_data: RevisionData, new_data: RevisionData) {
const old_title = old_data.title || ""
const new_title = new_data.title || ""
let added_chars = 0
let removed_chars = 0
let unchanged_chars = 0

let new_lines = 0
let modified_lines = 0
let deleted_lines = 0
const changes = diffChars(old_data.content.toLowerCase(), new_data.content.toLowerCase())

if (old_title != new_title) {
modified_lines++
}

// split into individual chars
const old_text = [...old_data.content.toLowerCase()]
const new_text = [...new_data.content.toLowerCase()]

const m = old_text.length
const n = new_text.length

// create a DP table of size (m+1) x (n+1)
// dp[i][j] represents the edit distance between old_text[0..i-1] and new_text[0..j-1]
const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0))

// base cases
for (let i = 0; i <= m; i++) {
dp[i][0] = i // deleting all lines from old_text
}
for (let j = 0; j <= n; j++) {
dp[0][j] = j // adding all lines from new_text
}

// build the DP table
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
if (old_text[i - 1] === new_text[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] // no change
} else {
dp[i][j] = Math.min(
dp[i - 1][j] + 1, // delete old line
dp[i][j - 1] + 1, // add new line
dp[i - 1][j - 1] + 1 // modify line
)
}
}
}

// backtrack to count the number of new, modified, and deleted lines
let i = m
let j = n
while (i > 0 && j > 0) {
if (old_text[i - 1] === new_text[j - 1]) {
// if lines are the same, move diagonally
i--
j--
} else if (dp[i][j] === dp[i - 1][j] + 1) {
// line deleted from old_text
deleted_lines++
i--
} else if (dp[i][j] === dp[i][j - 1] + 1) {
// line added in new_text
new_lines++
j--
for (const part of changes) {
if (part.added) {
added_chars += part.count || 0
} else if (part.removed) {
removed_chars += part.count || 0
} else {
modified_lines++
i--
j--
unchanged_chars += part.count || 0
}
}

// if there are remaining lines in old_text, they are deleted
while (i > 0) {
deleted_lines++
i--
}

// if there are remaining lines in new_text, they are added
while (j > 0) {
new_lines++
j--
}

return { new_lines, modified_lines, deleted_lines }
// A "modification" at the character level isn't a standard metric,
// it's usually just represented as a removal followed by an addition.
return { added_chars, removed_chars, unchanged_chars }
}
8 changes: 4 additions & 4 deletions src/background/pull_feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async function append_revision(
}

const now = Date.now()
const { new_lines, modified_lines, deleted_lines } = await calculate_new_metrics(
const { added_chars, removed_chars, unchanged_chars } = await calculate_new_metrics(
prev_data_obj,
rev_object
)
Expand All @@ -74,9 +74,9 @@ async function append_revision(
guid,
prev_data_obj,
now,
new_lines,
modified_lines,
deleted_lines
added_chars,
removed_chars,
unchanged_chars
)

return data.rev_id
Expand Down
8 changes: 7 additions & 1 deletion src/background/set_default_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export async function init_settings(): Promise<void> {
// modules
launcher_module: true,
launcher_module_shortcut: true,
reduce_timetable_width: false,

news_search_module: true,

recents_list_module: true,
record_post_history: true,
record_setting_active: false,
Expand All @@ -30,7 +33,10 @@ export async function init_settings(): Promise<void> {
await poll_feed()
}

export async function set_setting<K extends keyof Settings>(key: K, value: Settings[K]): Promise<void> {
export async function set_setting<K extends keyof Settings>(
key: K,
value: Settings[K]
): Promise<void> {
const current_settings = await chrome.storage.sync.get("settings")
if (!current_settings.settings) {
console.error("Settings not initialized yet.")
Expand Down
19 changes: 16 additions & 3 deletions src/content/modules/launcher/launch_homepage.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { get_stored_settings } from "../../../background/functions/utls/is_page"
import { poll_feed } from "../../../background/pull_feed"
import { store_classes } from "./getters/get_classes"
import { get_news_channels } from "./main"
import setup_launcher from "./setup_launcher"

// Function to inject the launcher
function inject_launcher(): void {
// Check if launcher already exists
if (document.getElementById("schoolbox-launcher")) {
async function inject_launcher(): Promise<void> {
if (document.getElementById("ultrabox-launcher-injected-marker")) {
// launcher already injected, no need to inject again
return
}

const marker = document.createElement("div")
marker.id = "ultrabox-launcher-injected-marker"
document.body.appendChild(marker)

// Check if launcher already exists
const settings = await get_stored_settings()
get_news_channels()
poll_feed()

Expand All @@ -24,6 +32,11 @@ function inject_launcher(): void {
launcher_content_div.id = "schoolbox-launcher-content-div"
launcher_content_div.className = "schoolbox-launcher-content-div"
launcher_div.appendChild(launcher_content_div)
launcher_div.style.marginTop = "40px"

if (settings?.reduce_timetable_width) {
launcher_div.style.width = "min(900px, 90%)"
}

// move elements on the main page to this new launcher div
// greeting heading
Expand Down
11 changes: 0 additions & 11 deletions src/content/modules/launcher/launcher_styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@
color: inherit;
}

#schoolbox-launcher-background-div {
display: flex;
justify-content: center;
align-items: flex-start;
}

#schoolbox-launcher-content-div {
width: min(900px, 90%);
margin-top: 40px;
}

.Component_Dashboard_GreetingController h1 {
margin: 0;
}
Expand Down
Loading
Loading