-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from csfloat/feature/auto-trading-cookies
Implements Auto Trading with Cookies
- Loading branch information
Showing
10 changed files
with
287 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {SimpleHandler} from './main'; | ||
import {RequestType} from './types'; | ||
|
||
export interface HasPermissionsRequest { | ||
permissions: string[]; | ||
} | ||
|
||
export interface HasPermissionsResponse { | ||
granted: boolean; | ||
} | ||
|
||
export const HasPermissions = new SimpleHandler<HasPermissionsRequest, HasPermissionsResponse>( | ||
RequestType.HAS_PERMISSIONS, | ||
async (req) => { | ||
// @ts-ignore | ||
const granted = (await chrome.permissions.contains({ | ||
permissions: req.permissions, | ||
})) as boolean; | ||
|
||
return { | ||
granted, | ||
}; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {SimpleHandler} from './main'; | ||
import {RequestType} from './types'; | ||
import {setupCookieAlarm} from '../../utils/alarm'; | ||
|
||
export interface SendCookiesRequest {} | ||
|
||
export interface SendCookiesResponse {} | ||
|
||
export const SendCookies = new SimpleHandler<SendCookiesRequest, SendCookiesResponse>( | ||
RequestType.SEND_COOKIES, | ||
async (req) => { | ||
const cookies = await chrome.cookies.getAll({ | ||
domain: 'steamcommunity.com', | ||
}); | ||
|
||
// For use in verifying trades on CSFloat, opt-in | ||
const formatted = cookies | ||
.filter((e) => { | ||
return [ | ||
'timezoneOffset', | ||
'Steam_Language', | ||
'browserid', | ||
'sessionid', | ||
'steamCountry', | ||
'steamLoginSecure', | ||
].includes(e.name); | ||
}) | ||
.map((e) => { | ||
return { | ||
name: e.name, | ||
value: e.value, | ||
expiration: e.expirationDate, | ||
}; | ||
}); | ||
|
||
const resp = await fetch(`https://csfloat.com/api/v1/me/steam-cookies`, { | ||
credentials: 'include', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
cookies: formatted, | ||
}), | ||
}); | ||
|
||
// Check if an alarm is setup | ||
await setupCookieAlarm(); | ||
|
||
return {} as SendCookiesResponse; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import {css, html} from 'lit'; | ||
|
||
import {CustomElement, InjectAfter, InjectionMode} from '../injectors'; | ||
import {FloatElement} from '../custom'; | ||
import '../common/ui/steam-button'; | ||
import {ClientSend} from '../../bridge/client'; | ||
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:not(.responsive_createtradeoffer)', | ||
InjectionMode.ONCE | ||
) | ||
export class AutoTrackWidget extends FloatElement { | ||
@state() | ||
show = false; | ||
|
||
static styles = [ | ||
...FloatElement.styles, | ||
css` | ||
.container { | ||
margin-top: 10px; | ||
margin-bottom: 10px; | ||
padding: 15px; | ||
background-color: rgb(48, 48, 48); | ||
color: white; | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
} | ||
.container.warning { | ||
background-color: rgb(179, 0, 0); | ||
} | ||
.float-icon { | ||
float: left; | ||
} | ||
.item-name { | ||
font-size: 18px; | ||
margin-left: 15px; | ||
line-height: 32px; | ||
} | ||
.sale-info { | ||
padding-left: 45px; | ||
color: darkgrey; | ||
} | ||
`, | ||
]; | ||
|
||
async connectedCallback() { | ||
super.connectedCallback(); | ||
|
||
try { | ||
await ClientSend(FetchPendingTrades, {}); | ||
|
||
const hasPermissions = await ClientSend(HasPermissions, {permissions: ['cookies', 'alarms']}); | ||
if (!hasPermissions) { | ||
this.show = true; | ||
} | ||
} catch (e) { | ||
console.info('user is not logged into CSFloat'); | ||
} | ||
} | ||
|
||
render() { | ||
return this.show | ||
? html` | ||
<div class="container" style="margin: 20px 0 20px 0;"> | ||
<div> | ||
<div class="float-icon"> | ||
<img | ||
src="https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/79/798a12316637ad8fbb91ddb7dc63f770b680bd19_full.jpg" | ||
style="height: 32px;" | ||
/> | ||
</div> | ||
<span class="item-name"> Automatically Track Offers </span> | ||
<div class="sale-info">Allow CSFloat Market to automatically track and create offers.</div> | ||
</div> | ||
<csfloat-steam-button | ||
id="csfloat-enable-tracking" | ||
.text="${'Enable Tracking'}" | ||
></csfloat-steam-button> | ||
</div> | ||
` | ||
: html``; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,43 @@ | ||
import {init} from './utils'; | ||
import '../components/trade_offers/offer_id'; | ||
import '../components/trade_offers/auto_track'; | ||
import {inPageContext} from '../utils/snips'; | ||
import {ClientSend} from '../bridge/client'; | ||
import {SendCookies} from '../bridge/handlers/send_cookies'; | ||
|
||
init('src/lib/page_scripts/trade_offers.js', main); | ||
|
||
function main() {} | ||
|
||
if (!inPageContext()) { | ||
const refresh = setInterval(() => { | ||
const widget = document.getElementsByTagName('csfloat-auto-track-widget'); | ||
if (!widget || widget.length === 0) { | ||
return; | ||
} | ||
|
||
const btn = widget[0]?.shadowRoot?.getElementById('csfloat-enable-tracking'); | ||
if (!btn) { | ||
return; | ||
} | ||
|
||
btn.addEventListener('click', async () => { | ||
chrome.runtime.sendMessage( | ||
{ | ||
message: 'requestPermissions', | ||
permissions: ['cookies', 'alarms'], | ||
}, | ||
(granted) => { | ||
if (granted) { | ||
ClientSend(SendCookies, {}); | ||
widget[0].parentElement?.removeChild(widget[0]); | ||
} else { | ||
alert('Failed to obtain permissions'); | ||
} | ||
} | ||
); | ||
}); | ||
|
||
clearInterval(refresh); | ||
}, 500); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || !chrome.alarms) { | ||
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({}, {}); | ||
}); | ||
} |