Skip to content

Commit

Permalink
Re-order indie wiki results in Google
Browse files Browse the repository at this point in the history
  • Loading branch information
jayktaylor authored Mar 21, 2024
1 parent cbdd4cd commit 39e44da
Show file tree
Hide file tree
Showing 26 changed files with 694 additions and 503 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.DS_Store
.vscode/*
*.py
*.pyc
*.pyc
.idea/
111 changes: 68 additions & 43 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ if (typeof importScripts !== 'undefined') {
importScripts('scripts/common-functions.js');
}

let cachedStorage = {};

async function updateCachedStorage() {
let localStorage = await chrome.storage.local.get();
let syncStorage = await chrome.storage.sync.get();
cachedStorage = {...localStorage, ...syncStorage};
}

async function getCachedStorage() {
if (Object.keys(cachedStorage).length === 0) {
await updateCachedStorage();
}
return cachedStorage;
}

// Cache Chrome's storage in memory so that we don't need to make repeated calls
updateCachedStorage();

// Capture web requests
chrome.webRequest.onBeforeSendHeaders.addListener(
async (event) => {
Expand All @@ -21,6 +39,11 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.action === 'updateIcon') {
setPowerIcon(msg.value);
} else if (msg.action === 'getStorage') {
getCachedStorage().then((res) => {
sendResponse(res)
});
return true;
}
});

Expand All @@ -31,6 +54,11 @@ chrome.runtime.onStartup.addListener(() => {
});
});

// Listen for changes to stored data, and updated our cached data
chrome.storage.onChanged.addListener(() => {
updateCachedStorage();
})

// Listen for extension installed/updating
chrome.runtime.onInstalled.addListener(async (detail) => {
// Set initial icon state
Expand Down Expand Up @@ -150,49 +178,46 @@ function redirectToBreezeWiki(storage, tabId, url) {
}

async function main(url, tabId) {
chrome.storage.local.get((localStorage) => {
chrome.storage.sync.get(async (syncStorage) => {
const storage = { ...syncStorage, ...localStorage };
if ((storage.power ?? 'on') === 'on') {
let crossLanguageSetting = storage.crossLanguage || 'off';
let matchingSite = await commonFunctionFindMatchingSite(url, crossLanguageSetting);

if (matchingSite) {
// Get user's settings for the wiki
let settings = await commonFunctionDecompressJSON(storage.wikiSettings) || {};
let id = matchingSite['id'];
let siteSetting = settings[id] || storage.defaultWikiAction || 'alert';

// Check if redirects are enabled for the site
if (siteSetting === 'redirect') {
let newURL = commonFunctionGetNewURL(url, matchingSite);

// Perform redirect
chrome.tabs.update(tabId, { url: newURL });

// Increase redirect count
chrome.storage.sync.set({ 'countRedirects': (storage.countRedirects ?? 0) + 1 });

// Notify if enabled
if ((storage.notifications ?? 'on') === 'on') {
// Notify that user is being redirected
let notifID = 'independent-wiki-redirector-notification-' + Math.floor(Math.random() * 1E16);
chrome.notifications.create(notifID, {
"type": "basic",
"iconUrl": 'images/logo-48.png',
"title": "You've been redirected!",
"message": "Indie Wiki Buddy has sent you from " + matchingSite['origin'] + " to " + matchingSite['destination']
});
// Self-clear notification after 6 seconds
setTimeout(() => { chrome.notifications.clear(notifID); }, 6000);
}
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
redirectToBreezeWiki(storage, tabId, url);
}
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
redirectToBreezeWiki(storage, tabId, url);
let storage = await getCachedStorage();

if ((storage.power ?? 'on') === 'on') {
let crossLanguageSetting = storage.crossLanguage || 'off';
let matchingSite = await commonFunctionFindMatchingSite(url, crossLanguageSetting);

if (matchingSite) {
// Get user's settings for the wiki
let settings = await commonFunctionDecompressJSON(storage.wikiSettings) || {};
let id = matchingSite['id'];
let siteSetting = settings[id] || storage.defaultWikiAction || 'alert';

// Check if redirects are enabled for the site
if (siteSetting === 'redirect') {
let newURL = commonFunctionGetNewURL(url, matchingSite);

// Perform redirect
chrome.tabs.update(tabId, { url: newURL });

// Increase redirect count
chrome.storage.sync.set({ 'countRedirects': (storage.countRedirects ?? 0) + 1 });

// Notify if enabled
if ((storage.notifications ?? 'on') === 'on') {
// Notify that user is being redirected
let notifID = 'independent-wiki-redirector-notification-' + Math.floor(Math.random() * 1E16);
chrome.notifications.create(notifID, {
"type": "basic",
"iconUrl": 'images/logo-48.png',
"title": "You've been redirected!",
"message": "Indie Wiki Buddy has sent you from " + matchingSite['origin'] + " to " + matchingSite['destination']
});
// Self-clear notification after 6 seconds
setTimeout(() => { chrome.notifications.clear(notifID); }, 6000);
}
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
redirectToBreezeWiki(storage, tabId, url);
}
});
});
} else if ((storage.breezewiki ?? 'off') === 'on' || (storage.breezewiki ?? 'off') === 'redirect') {
redirectToBreezeWiki(storage, tabId, url);
}
}
}
7 changes: 5 additions & 2 deletions data/sitesDE.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"destination_icon": "arkwiki.png",
"destination_main_page": "ARK_Wiki",
"destination_search_path": "/index.php",
"destination_content_path": "/wiki/",
"tags": [
"wiki.gg",
"official"
Expand Down Expand Up @@ -73,7 +74,8 @@
"destination_platform": "mediawiki",
"destination_icon": "minecraftwiki.png",
"destination_main_page": "Minecraft_Wiki",
"destination_search_path": "/"
"destination_search_path": "/",
"destination_content_path": "/w/"
},
{
"id": "de-pokemon",
Expand Down Expand Up @@ -128,6 +130,7 @@
"destination_icon": "terrariawiki.png",
"destination_main_page": "Terraria_Wiki",
"destination_search_path": "/index.php",
"destination_content_path": "/wiki/",
"tags": [
"wiki.gg",
"official"
Expand All @@ -151,4 +154,4 @@
"destination_main_page": "Hauptseite",
"destination_search_path": "/index.php"
}
]
]
Loading

0 comments on commit 39e44da

Please sign in to comment.