Skip to content

Commit

Permalink
Periodically Updates Cookie in Background
Browse files Browse the repository at this point in the history
  • Loading branch information
Step7750 committed Apr 4, 2024
1 parent 3e87a4c commit 42cb685
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {Handle} from './lib/bridge/server';
import {InternalResponseBundle} from './lib/bridge/types';
import MessageSender = chrome.runtime.MessageSender;
import {ClientSend} from './lib/bridge/client';
import {SendCookies} from './lib/bridge/handlers/send_cookies';
import {setupCookieAlarm} from './lib/utils/alarm';

function unifiedHandler(request: any, sender: MessageSender, sendResponse: (response?: any) => void) {
Handle(request, sender)
Expand Down Expand Up @@ -39,3 +42,7 @@ chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) =>
unifiedHandler(request, sender, sendResponse);
return true;
});

chrome.runtime.onInstalled.addListener(async ({reason}) => {
await setupCookieAlarm(true);
});
2 changes: 1 addition & 1 deletion src/lib/bridge/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function ClientSend<Req, Resp>(handler: RequestHandler<Req, Resp>,

// @ts-ignore Bad types
runtimeNamespace().runtime.sendMessage(
window.CSFLOAT_EXTENSION_ID || chrome.runtime.id,
(typeof window !== 'undefined' && window.CSFLOAT_EXTENSION_ID) || chrome.runtime.id,
bundle,
// @ts-ignore Bad types
(resp: InternalResponseBundle) => {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/bridge/handlers/send_cookies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {SimpleHandler} from './main';
import {RequestType} from './types';
import {setupCookieAlarm} from '../../utils/alarm';

export interface SendCookiesRequest {}

Expand Down Expand Up @@ -43,6 +44,9 @@ export const SendCookies = new SimpleHandler<SendCookiesRequest, SendCookiesResp
}),
});

// Check if an alarm is setup
await setupCookieAlarm();

return {} as SendCookiesResponse;
}
);
6 changes: 4 additions & 2 deletions src/lib/components/trade_offers/auto_track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {CustomElement, InjectAfter, InjectionMode} from '../injectors';
import {FloatElement} from '../custom';
import '../common/ui/steam-button';
import {ClientSend} from '../../bridge/client';
import {SendCookies} from '../../bridge/handlers/send_cookies';
import {state} from 'lit/decorators.js';
import {FetchPendingTrades} from '../../bridge/handlers/fetch_pending_trades';
import {HasPermissions} from '../../bridge/handlers/has_permissions';

@CustomElement()
@InjectAfter('.maincontent .profile_leftcol .nonresponsive_hidden', InjectionMode.ONCE)
@InjectAfter(
'.maincontent .profile_leftcol .nonresponsive_hidden:not(.responsive_createtradeoffer)',
InjectionMode.ONCE
)
export class AutoTrackWidget extends FloatElement {
@state()
show = false;
Expand Down
42 changes: 42 additions & 0 deletions src/lib/utils/alarm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {SendCookies} from '../bridge/handlers/send_cookies';

const COOKIE_ALARM_NAME = 'send-cookie-alarm';

// MUST be called from the background script
export async function setupCookieAlarm(initial = false) {
// @ts-ignore
const granted = (await chrome.permissions.contains({
permissions: ['alarms', 'cookies'],
})) as boolean;

if (!granted) {
return;
}

const existingAlarm = await chrome.alarms.get(COOKIE_ALARM_NAME);
if (existingAlarm) {
if (initial) {
createAlarmListener();
}

// Already exists, return
return;
}

await chrome.alarms.create(COOKIE_ALARM_NAME, {
delayInMinutes: 1,
periodInMinutes: 60 * 6, // 6 hours
});

createAlarmListener();
}

function createAlarmListener() {
chrome.alarms.onAlarm.addListener(async (alarm) => {
if (alarm.name !== COOKIE_ALARM_NAME) {
return;
}

await SendCookies.handleRequest({}, {});
});
}

0 comments on commit 42cb685

Please sign in to comment.