Skip to content

Commit 6ee9fad

Browse files
committed
feat add js info
1 parent 767f810 commit 6ee9fad

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Simple example demonstrating cookies usage with SmartScraper.
3+
*
4+
* This example shows the basic pattern for using cookies with the API.
5+
*/
6+
7+
import { smartScraper } from 'scrapegraph-js';
8+
import 'dotenv/config';
9+
10+
const apiKey = process.env.SGAI_APIKEY;
11+
12+
// Example cookies for authentication
13+
const cookies = {
14+
session_id: 'abc123def456',
15+
auth_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
16+
user_preferences: 'dark_mode,usd'
17+
};
18+
19+
async function scrapeWithCookies() {
20+
try {
21+
const response = await smartScraper(
22+
apiKey,
23+
'https://example.com/dashboard',
24+
'Extract user profile information',
25+
null, // schema
26+
null, // numberOfScrolls
27+
null, // totalPages
28+
cookies // cookies parameter
29+
);
30+
31+
console.log('✅ Scraping with cookies completed successfully');
32+
console.log(JSON.stringify(response, null, 2));
33+
34+
} catch (error) {
35+
console.error('❌ Error:', error.message);
36+
}
37+
}
38+
39+
// Run the example
40+
scrapeWithCookies();

scrapegraph-js/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "scrapegraph-js",
33
"author": "ScrapeGraphAI",
44
"version": "0.1.1",
5-
"description": "Scrape and extract structured data from a webpage using ScrapeGraphAI's APIs.",
5+
"description": "Scrape and extract structured data from a webpage using ScrapeGraphAI's APIs. Supports cookies for authentication, infinite scrolling, and pagination.",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/ScrapeGraphAI/scrapegraph-sdk",
@@ -22,7 +22,12 @@
2222
"gpt-3",
2323
"gpt-4",
2424
"llm",
25-
"ai"
25+
"ai",
26+
"cookies",
27+
"authentication",
28+
"session-management",
29+
"infinite-scroll",
30+
"pagination"
2631
],
2732
"main": "index.js",
2833
"module": "index.js",
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Test file to verify cookies integration functionality.
3+
*/
4+
5+
import { smartScraper } from './src/smartScraper.js';
6+
7+
function testCookiesIntegration() {
8+
console.log('🧪 Testing Cookies Integration');
9+
console.log('='.repeat(50));
10+
11+
// Test 1: Basic cookies validation
12+
console.log('\n1. Testing basic cookies validation...');
13+
14+
const cookies = { session_id: 'abc123', auth_token: 'xyz789' };
15+
16+
// Create a mock payload to test the logic
17+
const mockPayload = {
18+
website_url: 'https://httpbin.org/cookies',
19+
user_prompt: 'Extract cookie information'
20+
};
21+
22+
// Simulate the cookies validation logic
23+
if (cookies) {
24+
if (typeof cookies === 'object' && cookies !== null) {
25+
mockPayload.cookies = cookies;
26+
console.log('✅ Cookies validation passed');
27+
console.log(`✅ Cookies included: ${JSON.stringify(mockPayload.cookies)}`);
28+
} else {
29+
console.log('❌ Cookies validation failed - not an object');
30+
}
31+
}
32+
33+
// Test 2: Complex cookies scenario
34+
console.log('\n2. Testing complex cookies scenario...');
35+
36+
const complexCookies = {
37+
session_id: 'abc123def456',
38+
user_id: 'user789',
39+
auth_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
40+
preferences: 'dark_mode,usd',
41+
cart_id: 'cart101112',
42+
csrf_token: 'csrf_xyz789'
43+
};
44+
45+
const complexPayload = {
46+
website_url: 'https://example.com/dashboard',
47+
user_prompt: 'Extract user profile and preferences'
48+
};
49+
50+
if (complexCookies) {
51+
if (typeof complexCookies === 'object' && complexCookies !== null) {
52+
complexPayload.cookies = complexCookies;
53+
console.log('✅ Complex cookies validation passed');
54+
console.log(`✅ Complex cookies count: ${Object.keys(complexPayload.cookies).length}`);
55+
}
56+
}
57+
58+
// Test 3: Invalid cookies
59+
console.log('\n3. Testing invalid cookies...');
60+
61+
const invalidCookies = 'not_an_object';
62+
63+
try {
64+
if (invalidCookies) {
65+
if (typeof invalidCookies === 'object' && invalidCookies !== null) {
66+
console.log('❌ Should have failed validation');
67+
} else {
68+
console.log('✅ Invalid cookies correctly rejected');
69+
}
70+
}
71+
} catch (error) {
72+
console.log('✅ Error handling works correctly');
73+
}
74+
75+
// Test 4: Function signature validation
76+
console.log('\n4. Testing function signature...');
77+
78+
// Check if the function accepts the cookies parameter
79+
const functionString = smartScraper.toString();
80+
if (functionString.includes('cookies = null')) {
81+
console.log('✅ Function signature includes cookies parameter');
82+
} else {
83+
console.log('❌ Function signature missing cookies parameter');
84+
}
85+
86+
console.log('\n' + '='.repeat(50));
87+
console.log('✅ All cookies integration tests completed!');
88+
console.log('='.repeat(50));
89+
}
90+
91+
// Run the test
92+
testCookiesIntegration();

0 commit comments

Comments
 (0)