Skip to content

Commit

Permalink
feat: search on WebCite
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Jul 18, 2023
1 parent 9b52f7c commit d4e653d
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/action/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export default {
// Samsung Internet 18: tabs.getCurrent returns a tab
// instead of undefined, and tab.id refers to a nonexistent tab.
if (
isValidTab(currentTab) &&
(await isValidTab({tab: currentTab})) &&
!this.$env.isSafari &&
!this.$env.isSamsung
) {
Expand Down
14 changes: 14 additions & 0 deletions src/assets/icons/engines/webcite-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions src/assets/icons/engines/webcite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions src/assets/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
"description": "Name of the search engine."
},

"engineName_webcite": {
"message": "WebCite",
"description": "Name of the search engine."
},

"engineName_allEngines": {
"message": "All search engines",
"description": "Name of the search engine."
Expand Down Expand Up @@ -160,6 +165,11 @@
"description": "Title of the menu item."
},

"menuItemTitle_webcite": {
"message": "WebCite",
"description": "Title of the menu item."
},

"menuItemTitle_allEngines": {
"message": "All search engines",
"description": "Title of the menu item."
Expand Down Expand Up @@ -261,6 +271,11 @@
"description": "Title of the option."
},

"optionTitle_WebCite": {
"message": "WebCite",
"description": "Title of the option."
},

"optionTitle_searchMode": {
"message": "Search mode",
"description": "Title of the option."
Expand Down
8 changes: 5 additions & 3 deletions src/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ async function getTabUrl(session, search, doc, taskId) {
'{date}',
date.toISOString().split('.')[0].replace(/[-T:]/g, '')
);
} else if (engine === 'webcite') {
tabUrl = tabUrl.replace('{date}', new Date().toISOString().split('T')[0]);
}

tabUrl = tabUrl.replace(/{url}/g, url);
Expand Down Expand Up @@ -507,7 +509,7 @@ async function openCurrentDoc({linkUrl} = {}) {
}

async function onContextMenuItemClick(info, tab) {
if (targetEnv === 'samsung' && isValidTab(tab)) {
if (targetEnv === 'samsung' && (await isValidTab({tab}))) {
// Samsung Internet 13: contextMenus.onClicked provides wrong tab index.
tab = await browser.tabs.get(tab.id);
}
Expand Down Expand Up @@ -540,7 +542,7 @@ async function onActionClick(session, docUrl) {
}

async function onActionButtonClick(tab) {
if (targetEnv === 'samsung' && isValidTab(tab)) {
if (targetEnv === 'samsung' && (await isValidTab({tab}))) {
// Samsung Internet 13: browserAction.onClicked provides wrong tab index.
tab = await browser.tabs.get(tab.id);
}
Expand Down Expand Up @@ -762,7 +764,7 @@ async function processMessage(request, sender) {
sender.tab = null;
}

if (isValidTab(sender.tab)) {
if (await isValidTab({tab: sender.tab})) {
// Samsung Internet 13: runtime.onMessage provides wrong tab index.
sender.tab = await browser.tabs.get(sender.tab.id);
}
Expand Down
3 changes: 2 additions & 1 deletion src/storage/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"20230703074609_remove_gigablast",
"20230704125533_remove_opencurrentdocaction",
"20230713165504_add_perma.cc",
"20230715152710_add_ghostarchive"
"20230715152710_add_ghostarchive",
"20230718120215_add_webcite"
],
"sync": []
}
Expand Down
21 changes: 21 additions & 0 deletions src/storage/revisions/local/20230718120215_add_webcite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const message = 'Add WebCite';

const revision = '20230718120215_add_webcite';

async function upgrade() {
const changes = {};
const {engines, disabledEngines} = await browser.storage.local.get([
'engines',
'disabledEngines'
]);

const newEngines = ['webcite'];

changes.engines = engines.concat(newEngines);
changes.disabledEngines = disabledEngines.concat(newEngines);

changes.storageVersion = revision;
return browser.storage.local.set(changes);
}

export {message, revision, upgrade};
6 changes: 6 additions & 0 deletions src/tools/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ function main() {
docUrl = document.querySelector('._livepage a')?.href;
} else if (engine === 'ghostarchive') {
docUrl = document.querySelector('#searchInput')?.value;
} else if (engine === 'webcite') {
docUrl = document
.querySelector('frame[name="nav"]')
?.contentDocument.querySelector(
'tr:first-child td:nth-child(2) a'
)?.href;
}

break;
Expand Down
12 changes: 8 additions & 4 deletions src/utils/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,13 @@ function normalizeUrl(url) {
return parsedUrl.toString();
}

async function getOpenerTabId(openerTab) {
if (isValidTab(openerTab) && !(await getPlatform()).isMobile) {
return openerTab.id;
async function getOpenerTabId({tab, tabId = null} = {}) {
if (!tab && tabId !== null) {
tab = await browser.tabs.get(tabId).catch(err => null);
}

if ((await isValidTab({tab})) && !(await getPlatform()).isMobile) {
return tab.id;
}

return null;
Expand All @@ -182,7 +186,7 @@ async function showPage({
const props = {url, index: activeTab.index + 1, active: true, getTab};

if (setOpenerTab) {
props.openerTabId = await getOpenerTabId(activeTab);
props.openerTabId = await getOpenerTabId({tab: activeTab});
}

return createTab(props);
Expand Down
6 changes: 5 additions & 1 deletion src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,11 @@ function makeDocumentVisible() {
script.remove();
}

function isValidTab(tab) {
async function isValidTab({tab, tabId = null} = {}) {
if (!tab && tabId !== null) {
tab = await browser.tabs.get(tabId).catch(err => null);
}

if (tab && tab.id !== browser.tabs.TAB_ID_NONE) {
return true;
}
Expand Down
9 changes: 7 additions & 2 deletions src/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const engines = {
ghostarchive: {
target: 'https://ghostarchive.org/search?term={url}',
isExec: true
},
webcite: {
target: 'https://webcitation.org/query?url={url}&date={date}'
}
};

Expand All @@ -77,7 +80,8 @@ const engineIconAlias = {
const engineIconVariants = {
archiveOrg: ['dark'],
archiveIs: ['dark'],
yahoo: ['dark']
yahoo: ['dark'],
webcite: ['dark']
};

const rasterEngineIcons = ['ghostarchive'];
Expand Down Expand Up @@ -128,7 +132,8 @@ const pageArchiveHosts = {
bing: ['cc.bingj.com'],
yandex: ['yandexwebcache.net'],
permacc: ['perma.cc', 'rejouer.perma.cc'],
ghostarchive: ['ghostarchive.org']
ghostarchive: ['ghostarchive.org'],
webcite: ['webcitation.org']
};

const linkArchiveHosts = {
Expand Down

0 comments on commit d4e653d

Please sign in to comment.