Skip to content

Commit

Permalink
Merge branch 'ts-refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
Acorn221 committed Nov 5, 2022
2 parents 58a7b59 + 5d7a051 commit 0dedf77
Show file tree
Hide file tree
Showing 19 changed files with 1,980 additions and 398 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<script type="module" src="/src/popup/main.tsx"></script>
</body>
</html>
919 changes: 591 additions & 328 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@
"@mui/icons-material": "^5.3.0",
"@mui/material": "^5.3.0",
"@mui/styled-engine-sc": "^5.3.0",
"@types/chrome": "^0.0.193",
"@typescript-eslint/eslint-plugin": "^5.30.6",
"postcss": "^8.4.14",
"react": "^18.2.0",
"react-collapsible": "^2.10.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"react-world-flags": "^1.5.0",
"tailwindcss": "^3.1.6"
},
"devDependencies": {
"@crxjs/vite-plugin": "^1.0.12",
"@types/node": "^18.0.3",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/react-world-flags": "^1.4.2",
"@typescript-eslint/parser": "^5.30.6",
"@vitejs/plugin-react": "^1.3.0",
"autoprefixer": "^10.4.7",
Expand Down
18 changes: 16 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
# LighterFuel For Tinder

This chrome extension was initially made to get the date of when photos are uploaded to a tinder profile
This chrome extension was initially made to get the date of when photos are uploaded to a tinder profile.

It has had a JS -> TS refactor recently and lots of the types are not optimal.

# Features

- Get the date of when photos are uploaded to a tinder profile
- Get the high quality photos (not reliable, due to Tinder's API)
- Get the high quality photos (not reliable, due to Tinder's API)

# Installation

npm i --legacy-peer-deps

# How to run

npm run dev

# How to build

npm run build
240 changes: 240 additions & 0 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
import { debug } from '@/config.js';
import { AISettings, ImageType, ShowSettings } from '@/types';

class Background {
selfProfile: any;

aiSettings: {
temperature: number;
bestOf: number;
APIkey: string;
};

images: ImageType[];

showSettings: ShowSettings;

constructor() {
/**
* Profiles not used right now, however it will be in the future
* TODO: make profiles do something
*/

this.selfProfile = null;

// the array of image object which have been loaded
this.images = [];

this.showSettings = {
overlayButton: true,
searchButton: true,
enlargeButton: true,
};

this.aiSettings = {
temperature: 95,
bestOf: 1,
APIkey: '',
};

this.getStoredSettings();
this.initialiseImageListner();
this.initialisePagelistener();
this.initialisePopupListner();
}

/**
* Used to set the showOverlays variable
*/
setShowSettings(setting: ShowSettings, stored = false) {
if (!setting) console.error('setting is undefined :(');
if (!stored) chrome.storage.sync.set({ showSettings: setting });
this.showSettings = setting;
// bit hacky, should fix at some point, just to trigger the sending of the "show overlays" bool
// TODO: change this to an event emitter
this.sendInfoToTab({
action: 'settings update',
data: this.showSettings,
}).catch((e) => {
this.consoleOut(e);
});
this.consoleOut(`showOverlays set to ${setting}`);
}

setAiSettings(settings: AISettings, stored = false) {
if (!stored) chrome.storage.sync.set({ aiSettings: settings });
this.aiSettings = settings;
}

/**
* Initialises the listner for the popup
*/
initialisePopupListner() {
chrome.runtime.onConnect.addListener((port) => {
port.postMessage({ showSettings: this.showSettings });
port.onMessage.addListener((msg) => {
if ('showSettings' in msg) {
this.setShowSettings(msg.showSettings);
port.postMessage({ showSettings: this.showSettings });
} else if ('get ai settings' in msg) {
port.postMessage({ aiSettings: this.aiSettings });
} else if ('set ai settings' in msg) {
this.setAiSettings(msg['set ai settings']);
}
});
});
}

/**
* Initialises the page listener so the injected JS can communicate with this
*/
initialisePagelistener() {
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
this.consoleOut(request);

switch (request.action) {
case 'get initial data':
// images = images.filter(x => x.date > Date.now()-10000);// clears the old urls from the ram
sendResponse({ imageArray: this.images, showSettings: this.showSettings });
// sendResponse({"imageArray": images, "showOverlays": showOverlays});
break;
case 'send core':
this.consoleOut(request.core);
// not doing anything with this rn
break;
case 'send profile':
this.selfProfile = request.profile.data.user;
break;
case 'send matches':
if (!request.matches.data) {
this.consoleOut('send matches that was invalid to loop through arr:');
this.consoleOut(request.matches.data);
return;
}
break;
case 'send user data':
this.consoleOut('Got user data!');
this.consoleOut(request.data.results);
break;
case 'send messages':
if (!Array.isArray(request.messages.data.messages)) {
this.consoleOut('Error recieving messages :( they\'re not an array!');
return;
}
break;
case 'ping': // used for debugging
sendResponse('pong');
break
default:
sendResponse('I was supposed to be sent something :(');
break;
}
});
}

/**
* Used to listen out for whenever there is a request to the "core"
* which contains the profile information!
*/
initialiseCoreListner() {
chrome.webRequest.onCompleted.addListener(
(details) => {
console.log('core/profile:');
console.log(details);
},
{ urls: ['*://api.gotinder.com/v2/recs/core*', '*://api.gotinder.com/v2/profile*'] },
);
}

/**
* Initialised the image listner, for whenever tinder requests a new image,
* add it to the images arr (through proxy)
*/
initialiseImageListner() {
chrome.webRequest.onCompleted.addListener(
(details) => {
// tinder has yet to discover cache and the images reload *every time they're viewed*
const imageInArray = this.images.find((x) => details.url === x.url);
if (!imageInArray) {
if(!details.responseHeaders) return;
this.sendInfoToTab({
action: 'new image',
data: {
url: details.url,
lastModified: details.responseHeaders.filter((x) => x.name === 'last-modified')[0].value,
timeAddedToArr: Date.now(),
},
}).catch((e) => {
if (debug) console.log(e);
});
console.log(details);
}
},
{ urls: ['*://*.gotinder.com/*/*.jpg*', '*://*.gotinder.com/*/*.webp*', '*://*.gotinder.com/*/*.mp4*'] },
['responseHeaders'],
);
}

/**
* Gets the stored settings from chrome
*/
getStoredSettings() {
chrome.storage.sync.get(['showSettings'], (result) => {
if (!result.showSettings) {
// set the default settings
if (debug) console.log("No 'showSettings' Found, Setting To Default!");
this.setShowSettings({ overlayButton: true, searchButton: true, enlargeButton: true }, false);
} else {
// else set it to the default value
if (debug) console.log(`'showSettings' Found: ${JSON.stringify(result.showSettings)}`);
this.setShowSettings(result.showSettings, true);
}
});
chrome.storage.sync.get(['aiSettings'], (result) => {
if (result.aiSettings) {
this.aiSettings = result.aiSettings;
}
});
}

/**
* This filters through all the tabs and sends the info to them
*
*/
sendInfoToTab(obj: { action: string; data: ShowSettings | { url: string; lastModified: string | undefined; timeAddedToArr: number; }; }) {
// format {action: String. data: Object}
return new Promise<void>((resolve) => {
chrome.tabs.query({})// query all tabs
.then((x) => x.filter((y) => y.url))// filter to only ones we have permission to look at (all tinder tabs)
.then((tabs) => {
for (const tab of tabs) {
if(tab.id) chrome.tabs.sendMessage(tab.id, obj);
}
resolve();
});
});
}

/**
* a console log facade for the debug bool
*/
consoleOut(message: string | any, error = false) {
if (debug) {
if (error) {
console.error(message);
} else {
console.log(message);
}
}
}
}

try {
const bg = new Background();
// prints the bg instance to the console for debugging!
console.log(bg);
} catch (err: any) {
console.error(`Error caught in background.js: ${err.stack}`);
}

export default Background;
Loading

0 comments on commit 0dedf77

Please sign in to comment.