Skip to content

Commit

Permalink
Merge pull request #373 from ulixee/docUrl
Browse files Browse the repository at this point in the history
Various small bug fixes
  • Loading branch information
calebjclark authored Nov 13, 2021
2 parents a4f681e + b9275e6 commit b211ba3
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 4 deletions.
5 changes: 5 additions & 0 deletions client/lib/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const propertyKeys: (keyof Resource)[] = [
'type',
'request',
'response',
'documentUrl',
'data',
'json',
'text',
Expand All @@ -46,6 +47,10 @@ export default class Resource {
return getState(this).resource.url;
}

public get documentUrl(): string {
return getState(this).resource.documentUrl;
}

public get type(): ResourceType {
return getState(this).resource.type;
}
Expand Down
4 changes: 4 additions & 0 deletions client/lib/WebsocketResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export default class WebsocketResource extends AwaitedEventTarget<IEventType> {
return getState(this).resource.url;
}

public get documentUrl(): string {
return getState(this).resource.documentUrl;
}

public get type(): ResourceType {
return 'Websocket';
}
Expand Down
1 change: 1 addition & 0 deletions core/lib/SessionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ export default class SessionState {
receivedAtCommandId: this.lastCommand?.id,
type: resourceType,
isRedirect: !!redirectedToUrl,
documentUrl: resourceEvent.documentUrl,
request: {
...request,
postData: request.postData?.toString(),
Expand Down
4 changes: 3 additions & 1 deletion core/lib/UserProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default class UserProfile {
public static async export(session: Session) {
const cookies = await session.browserContext.getCookies();

const storage: IDomStorage = {};
// start with previous storage
const storage: IDomStorage = session.options.userProfile?.storage ?? {};

for (const tab of session.tabsById.values()) {
const page = tab.puppetPage;

Expand Down
4 changes: 4 additions & 0 deletions core/models/ResourcesTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class ResourcesTable extends SqliteTable<IResourcesRecord> {
['browserLoadFailure', 'TEXT'],
['browserBlockedReason', 'TEXT'],
['browserCanceled', 'INTEGER'],
['documentUrl', 'TEXT'],
],
true,
);
Expand Down Expand Up @@ -106,6 +107,7 @@ export default class ResourcesTable extends SqliteTable<IResourcesRecord> {
record.browserLoadFailure,
record.browserBlockedReason,
record.browserCanceled ? 1 : 0,
record.documentUrl,
]);
}

Expand Down Expand Up @@ -172,6 +174,7 @@ export default class ResourcesTable extends SqliteTable<IResourcesRecord> {
meta.response?.browserLoadFailure,
extras.browserBlockedReason,
extras.browserCanceled ? 1 : 0,
meta.documentUrl,
]);
}

Expand Down Expand Up @@ -256,4 +259,5 @@ export interface IResourcesRecord {
browserLoadFailure?: string;
browserBlockedReason?: string;
browserCanceled?: boolean;
documentUrl: string;
}
55 changes: 55 additions & 0 deletions core/test/user-profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,61 @@ document.querySelector('#session').innerHTML = [session1,session2,session3].join
await tab2.close();
});

it("should keep profile information for sites that aren't loaded in a session", async () => {
const meta = await connection.createSession({
userProfile: {
cookies: [],
storage: {
[koaServer.baseUrl]: {
indexedDB: [],
localStorage: [
['Test1', 'value0'],
['test2', 'value1'],
],
sessionStorage: [],
},
'https://previousSite.org': {
indexedDB: [],
localStorage: [['test', 'site1.org']],
sessionStorage: [],
},
'https://site2.org': {
indexedDB: [],
localStorage: [['test2', 'site2.org']],
sessionStorage: [],
},
},
},
});
const tab = Session.getTab(meta);
Helpers.needsClosing.push(tab.session);

koaServer.get('/unloaded', ctx => {
ctx.body = `<body>
<h1>storage page</h1>
<script>
localStorage.setItem('Test1', 'value1');
</script>
</body>`;
});

await tab.goto(`${koaServer.baseUrl}/unloaded`);
await tab.waitForLoad('PaintingStable');

const profile = await tab.session.exportUserProfile();
expect(profile.cookies).toHaveLength(0);
expect(profile.storage[koaServer.baseUrl]?.localStorage).toHaveLength(2);
expect(profile.storage[koaServer.baseUrl]?.localStorage.find(x => x[0] === 'Test1')).toEqual([
'Test1',
'value1',
]);
expect(profile.storage['https://previousSite.org'].localStorage).toEqual([
['test', 'site1.org'],
]);
expect(profile.storage['https://site2.org'].localStorage).toEqual([['test2', 'site2.org']]);
await tab.close();
});

it('should not make requests to end sites during profile "install"', async () => {
const mitmSpy = jest.spyOn(HttpRequestHandler, 'onRequest');
await connection.createSession({
Expand Down
1 change: 1 addition & 0 deletions interfaces/IResourceMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default interface IResourceMeta {
request: IResourceRequest;
response?: IResourceResponse;
type: ResourceType;
documentUrl: string;
isRedirect?: boolean;
receivedAtCommandId?: number;
seenAtCommandId?: number;
Expand Down
1 change: 1 addition & 0 deletions mitm/handlers/RequestSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export interface IRequestSessionResponseEvent extends IRequestSessionRequestEven
export interface IRequestSessionRequestEvent {
id: number;
request: IResourceRequest;
documentUrl: string;
serverAlpn: string;
protocol: string;
socketId: number;
Expand Down
1 change: 1 addition & 0 deletions mitm/lib/MitmRequestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export default class MitmRequestContext {
browserRequestId: ctx.browserRequestId,
request,
response,
documentUrl: ctx.documentUrl,
redirectedToUrl: ctx.redirectedToUrl,
wasCached: ctx.cacheHandler?.didProposeCachedResource ?? false,
resourceType: ctx.resourceType,
Expand Down
3 changes: 2 additions & 1 deletion plugins/default-human-emulator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ export default class DefaultHumanEmulator extends HumanEmulator {

const { nodeId } = targetRect;

const targetPoint = getRandomRectPoint(targetRect, DefaultHumanEmulator.boxPaddingPercent);
let targetPoint = getRandomRectPoint(targetRect, DefaultHumanEmulator.boxPaddingPercent);
const didMoveMouse = await this.moveMouseToPoint(targetPoint, targetRect.width, runFn, helper);
if (didMoveMouse) {
targetRect = await helper.lookupBoundingRect([nodeId], true, true);
targetPoint = getRandomRectPoint(targetRect, DefaultHumanEmulator.boxPaddingPercent);
}

if (targetRect.elementTag === 'option') {
Expand Down
1 change: 1 addition & 0 deletions puppet-chrome/lib/ProtocolError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class ProtocolError extends Error {
this.name = 'ProtocolError';
this.method = method;
this.stack = stack;
this.stack = `${this.name}: ${this.message}\n${stack}`;
this.remoteError = remoteError;
}
}
1 change: 1 addition & 0 deletions puppet/lib/PuppetLaunchError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export default class PuppetLaunchError extends Error implements IPuppetLaunchErr
super(message);
this.stack = stack;
this.name = 'PuppetLaunchError';
this.stack = `${this.name}: ${this.message}\n${stack}`;
}
}
6 changes: 6 additions & 0 deletions website/docs/Advanced/Resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Retrieve the network request used to retrieve this resource.

The requested url

### documentUrl

The document (if applicable) that requested this resource.

#### **Returns** `string`

#### **Returns** `string`

### type
Expand Down
2 changes: 0 additions & 2 deletions website/src/layouts/partials/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
| Repository
a(href="//github.com/ulixee/secret-agent/issues" rel="noopener noreferrer" target="_blank")
| Issues
a(href="//github.com/ulixee/secret-agent/projects/1" rel="noopener noreferrer" target="_blank")
| Roadmap
</template>

<static-query>
Expand Down

0 comments on commit b211ba3

Please sign in to comment.