Merge pull request 'refactor: remove the unused URL shortener' (#11) from refactor/remove-unused-shortener into develop

Reviewed-on: https://git.zavage.net/Zavage-Software/search-with-chatgpt-powered-by-openai-extension/pulls/11
This commit is contained in:
zavage
2026-10-18 02:26:37 -06:00
5 changed files with 43 additions and 175 deletions
-1
View File
@@ -13,7 +13,6 @@ const TITLES = {
};
export function getContext(info) {
if (info.inputType === 'password') return null;
if (typeof info.selectionText === 'string' && info.selectionText.trim()) {
return { kind: 'selection', source: info.selectionText.trim() };
}
+3 -62
View File
@@ -12,9 +12,6 @@ const BASE = "https://chatgpt.com/";
export const MAX_URL_CHARS = 8000;
// Fraction of the kept text within which a whitespace cut is preferred.
const WHITESPACE_WINDOW = 0.9;
export function applyTemplate(template, query) {
if (query === "") {
return "";
@@ -42,65 +39,9 @@ function addOptionalParams(url, settings) {
return url;
}
function lastWhitespaceIndex(text) {
for (let i = text.length - 1; i >= 0; i -= 1) {
if (/\s/.test(text[i])) {
return i;
}
}
return -1;
}
// Returns the longest prefix of `prompt` whose finished URL fits the budget.
// Slicing happens by CODE POINT on the decoded string, so a surrogate pair can
// never be split and a percent-escape can never be severed (encoding happens
// afterwards, via URLSearchParams).
function fitToBudget(prompt, settings) {
const codePoints = Array.from(prompt);
const urlLengthFor = (count) => {
const url = new URL(BASE);
url.searchParams.set("q", codePoints.slice(0, count).join(""));
addOptionalParams(url, settings);
return url.href.length;
};
if (urlLengthFor(codePoints.length) <= MAX_URL_CHARS) {
return prompt;
}
let low = 0;
let high = codePoints.length;
while (low < high) {
const mid = Math.floor((low + high + 1) / 2);
if (urlLengthFor(mid) <= MAX_URL_CHARS) {
low = mid;
} else {
high = mid - 1;
}
}
const kept = codePoints.slice(0, low).join("");
const boundary = Math.floor(kept.length * WHITESPACE_WINDOW);
const cut = lastWhitespaceIndex(kept);
return cut >= boundary && cut > 0 ? kept.slice(0, cut) : kept;
}
export function buildChatGptUrl(settings, rawQuery) {
const query = String(rawQuery ?? "").trim();
const prompt = fitToBudget(applyTemplate(settings.promptTemplate, query), settings);
// q first, matching the most-cited working examples. Total serialized length
// is order-independent, so measuring without q above is sound.
const url = new URL(BASE);
if (prompt !== "") {
url.searchParams.set("q", prompt);
}
addOptionalParams(url, settings);
return url.href;
}
// Encode a complete prompt without silently shortening it.
// Encode a complete prompt without silently shortening it; prepareAction owns
// the length budget and asks before shortening. q goes first, matching the
// most-cited working examples.
export function serializePrompt(settings, prompt) {
const url = new URL(BASE);
if (prompt) url.searchParams.set("q", prompt);