Skip to content
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Firefox addon that deletes history older than a specified amount of days.
* Interval in minutes between triggering.
* Only has effect if trigger mode is set to timer.
* Defaults to 1440 (24 hours), Minimum 1
* Regex Clean
* Manually preview & delete history entries whose URL matches a regular expression.
* Supports case-insensitive/unicode flags, live pattern validation.
* Results show page title, visit count, visit time, sortable by last visit, visit count, URL.
* Recent patterns saved for easy re-use later.
* Hidden by default. Enable the section from the Extras section of the popup.

## Permissions

Expand Down
24 changes: 24 additions & 0 deletions src/OptionsInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import browser from "./we";

export interface RegexHistoryEntry {
pattern: string;
flags: string;
}

/** Shape of options object */
export interface OptionsInterface extends Record<string, unknown> {
dirty: boolean;
Expand All @@ -10,6 +15,8 @@ export interface OptionsInterface extends Record<string, unknown> {
deleteMode: "idle" | "startup" | "timer" | string;
notifications: boolean;
downloads: boolean;
regexClean: boolean;
regexHistory?: RegexHistoryEntry[];
// filterHistory: boolean;
// filterList: string[];

Expand All @@ -28,6 +35,7 @@ export interface FormElements extends HTMLFormControlsCollection {
notifications: HTMLInputElement;
// notificationsPermission: HTMLInputElement;
downloads: HTMLInputElement;
regexClean: HTMLInputElement;
// downloadsPermission: HTMLInputElement;
// filterHistory: HTMLInputElement;
// filterList: HTMLTextAreaElement;
Expand All @@ -49,6 +57,8 @@ export class Options implements OptionsInterface {
deleteMode = "timer";
notifications = false;
downloads = false;
regexClean = false;
regexHistory: RegexHistoryEntry[] = [];
icon = "theme";
// filterHistory = false
// filterList = ["example.com", "example.org"]
Expand Down Expand Up @@ -110,6 +120,20 @@ export class Options implements OptionsInterface {
this.downloads = optionsObj.downloads;
}

if (typeof optionsObj.regexClean === "boolean") {
this.regexClean = optionsObj.regexClean;
}

if (Array.isArray(optionsObj.regexHistory)) {
const entries: RegexHistoryEntry[] = [];
for (const entry of optionsObj.regexHistory) {
if (typeof entry === "object" && entry !== null && typeof entry.pattern === "string" && typeof entry.flags === "string") {
entries.push({ pattern: entry.pattern, flags: entry.flags });
}
}
this.regexHistory = entries.slice(0, 10);
}

// if (typeof optionsObj.filterHistory === "boolean") {
// this.filterHistory = optionsObj.filterHistory;
// }
Expand Down
41 changes: 41 additions & 0 deletions src/RegexClean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import browser from "./we";

export const REGEX_SEARCH_CAP = 100000;

export interface RegexMatch {
url: string;
title: string;
lastVisitTime: number;
visitCount: number;
}

export interface RegexPreview {
items: RegexMatch[];
truncated: boolean;
}

export async function previewRegexClean(pattern: string, flags: string): Promise<RegexPreview> {
const re = new RegExp(pattern, flags);
const results = await browser.history.search({ text: "", startTime: 0, maxResults: REGEX_SEARCH_CAP });
const seen = new Set<string>();
const items: RegexMatch[] = [];
for (const item of results) {
if (typeof item.url === "string" && !seen.has(item.url) && re.test(item.url)) {
seen.add(item.url);
items.push({
url: item.url,
title: item.title ?? "",
lastVisitTime: item.lastVisitTime ?? 0,
visitCount: item.visitCount ?? 0
});
}
}
return { items, truncated: results.length >= REGEX_SEARCH_CAP };
}

export async function deleteUrls(urls: string[]): Promise<void> {
const chunkSize = 50;
for (let i = 0; i < urls.length; i += chunkSize) {
await Promise.all(urls.slice(i, i + chunkSize).map(url => browser.history.deleteUrl({ url })));
}
}
99 changes: 99 additions & 0 deletions src/RegexHighlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
type RegexTokenKind = "escape" | "charClass" | "quantifier" | "group" | "alternation" | "anchor" | "literal";

interface RegexToken {
text: string;
kind: RegexTokenKind;
}

function tokenizeRegex(pattern: string): RegexToken[] {
const tokens: RegexToken[] = [];
let i = 0;
let inClass = false;
while (i < pattern.length) {
const c = pattern[i];
if (c === "\\" && i + 1 < pattern.length) {
tokens.push({ text: pattern.slice(i, i + 2), kind: "escape" });
i += 2;
continue;
}
if (inClass) {
if (c === "]") {
inClass = false;
}
tokens.push({ text: c, kind: "charClass" });
i += 1;
continue;
}
if (c === "[") {
inClass = true;
tokens.push({ text: c, kind: "charClass" });
i += 1;
continue;
}
if (c === "(") {
if (pattern[i + 1] === "?") {
let j = i + 2;
while (j < pattern.length && ":=!<>".includes(pattern[j])) {
j += 1;
}
tokens.push({ text: pattern.slice(i, j), kind: "group" });
i = j;
} else {
tokens.push({ text: c, kind: "group" });
i += 1;
}
continue;
}
if (c === ")") {
tokens.push({ text: c, kind: "group" });
i += 1;
continue;
}
if (c === "{") {
const end = pattern.indexOf("}", i);
if (end !== -1 && /^\{\d+(,\d*)?\}$/.test(pattern.slice(i, end + 1))) {
tokens.push({ text: pattern.slice(i, end + 1), kind: "quantifier" });
i = end + 1;
continue;
}
tokens.push({ text: c, kind: "literal" });
i += 1;
continue;
}
if (c === "*" || c === "+" || c === "?") {
tokens.push({ text: c, kind: "quantifier" });
i += 1;
continue;
}
if (c === "|") {
tokens.push({ text: c, kind: "alternation" });
i += 1;
continue;
}
if (c === "^" || c === "$" || c === ".") {
tokens.push({ text: c, kind: "anchor" });
i += 1;
continue;
}
tokens.push({ text: c, kind: "literal" });
i += 1;
}
return tokens;
}

export function renderRegexTokens(container: HTMLElement, pattern: string): void {
container.replaceChildren();
let current: HTMLSpanElement | null = null;
let currentKind: RegexTokenKind | null = null;
for (const token of tokenizeRegex(pattern)) {
if (current !== null && token.kind === currentKind) {
current.innerText += token.text;
continue;
}
current = document.createElement("span");
currentKind = token.kind;
current.className = "re-" + token.kind;
current.innerText = token.text;
container.append(current);
}
}
116 changes: 116 additions & 0 deletions src/_locales/en_US/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,121 @@
"statisticsUnsavedChanges": {
"message": "History will not be cleared because there are unsaved changes.",
"description": ""
},
"optionRegexCleanCheckbox": {
"message": "Enable regex clean section",
"description": ""
},
"regexCleanSection": {
"message": "Regex Clean",
"description": ""
},
"regexCleanInfo": {
"message": "Preview/Delete history entries whose URL matches a regular expression.",
"description": ""
},
"regexCleanPatternLabel": {
"message": "Regular expression",
"description": ""
},
"regexCleanFlagIgnoreCase": {
"message": "Case insensitive (i)",
"description": ""
},
"regexCleanFlagUnicode": {
"message": "Unicode (u)",
"description": ""
},
"regexCleanRegex101": {
"message": "Open in regex101.com",
"description": ""
},
"regexCleanPreview": {
"message": "Preview Matches",
"description": ""
},
"regexCleanDelete": {
"message": "Delete Matches",
"description": ""
},
"regexCleanMatchCount": {
"message": "$COUNT$ matches",
"description": "",
"placeholders": {
"count": {
"content": "$1",
"example": ""
}
}
},
"regexCleanNoMatches": {
"message": "No matches",
"description": ""
},
"regexCleanSearchError": {
"message": "History search failed",
"description": ""
},
"regexCleanDeleted": {
"message": "Deleted $COUNT$ entries",
"description": "",
"placeholders": {
"count": {
"content": "$1",
"example": ""
}
}
},
"regexCleanDeleteError": {
"message": "History deletion failed",
"description": ""
},
"regexCleanTruncated": {
"message": "(capped at $CAP$)",
"description": "",
"placeholders": {
"cap": {
"content": "$1",
"example": ""
}
}
},
"regexCleanShowMore": {
"message": "Show $COUNT$ more",
"description": "",
"placeholders": {
"count": {
"content": "$1",
"example": ""
}
}
},
"regexCleanSortLabel": {
"message": "Sort by",
"description": ""
},
"regexCleanSortLastVisit": {
"message": "Last visit",
"description": ""
},
"regexCleanSortVisitCount": {
"message": "Visit count",
"description": ""
},
"regexCleanSortUrl": {
"message": "URL",
"description": ""
},
"regexCleanRecentPatterns": {
"message": "Recent patterns",
"description": ""
},
"regexCleanRemovePattern": {
"message": "Remove from recent patterns",
"description": ""
},
"regexCleanPopout": {
"message": "Open in window",
"description": ""
}
}
Loading