Skip to content

Commit 75238f6

Browse files
committed
use fetch to get reviews
1 parent eb3c7f9 commit 75238f6

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

public/campfire-commerce/script.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Fetch product data from JSON source
22
// blocked by https://github.com/lightpanda-io/browsercore/issues/187
33
// window.addEventListener("load", () => {
4+
5+
// use XHR to retrieve the product's infos.
46
const detailsXHR = new XMLHttpRequest();
57
// blocked by https://github.com/lightpanda-io/browsercore/issues/186
68
// detailsXHR.open('GET', 'json/product.json');
@@ -13,17 +15,19 @@ detailsXHR.open('GET', document.URL + 'json/product.json');
1315
};
1416
detailsXHR.send();
1517

16-
const reviewsXHR = new XMLHttpRequest();
17-
// blocked by https://github.com/lightpanda-io/browsercore/issues/186
18-
// reviewsXHR.open('GET', 'json/reviews.json');
19-
reviewsXHR.open('GET', document.URL + 'json/reviews.json');
20-
reviewsXHR.responseType = 'json';
21-
reviewsXHR.onload = function() {
22-
if (this.status === 200) {
23-
updateReviews(this.response);
18+
// use fetch to retrieve reviews.
19+
(async function () {
20+
try {
21+
const url = document.URL + 'json/reviews.json';
22+
const response = await fetch(url);
23+
if (!response.ok) {
24+
throw new Error(`Response status: ${response.status}`);
25+
}
26+
updateReviews(await response.json());
27+
} catch (error) {
28+
console.error(error.message);
2429
}
25-
};
26-
reviewsXHR.send();
30+
}());
2731

2832
// blocked by https://github.com/lightpanda-io/browsercore/issues/185
2933
//

puppeteer/issue365.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import puppeteer from "puppeteer-core"
2+
let url = "https://www.wikipedia.org/"
3+
4+
// use browserWSEndpoint to pass the Lightpanda's CDP server address.
5+
const browser = await puppeteer.connect({
6+
browserWSEndpoint: "ws://127.0.0.1:9222",
7+
})
8+
9+
// The rest of your script remains the same.
10+
const context = await browser.createBrowserContext()
11+
const page = await context.newPage()
12+
13+
await page.goto(url)
14+
15+
const html = await page.content()
16+
console.log("html :>> ", html)
17+
18+
await page.close()
19+
await context.close()
20+
await browser?.disconnect()

puppeteer/issue416.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
import puppeteer from 'puppeteer-core';
4+
5+
// use browserWSEndpoint to pass the Lightpanda's CDP server address.
6+
const browser = await puppeteer.connect({
7+
browserWSEndpoint: "ws://127.0.0.1:9222",
8+
});
9+
10+
// The rest of your script remains the same.
11+
const context = await browser.createBrowserContext();
12+
const page = await context.newPage();
13+
14+
await page.goto('https://wikipedia.com/');
15+
16+
await page.close();
17+
await context.close();

selenium/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Selenium examples using CDP
2+
3+
## Prerequisites
4+
5+
```
6+
$ pip install trio selenium
7+
```

selenium/cdp.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import trio
2+
import sys
3+
import logging
4+
from selenium import webdriver
5+
from selenium.webdriver.common.by import By
6+
7+
logger = logging.getLogger('selenium')
8+
logger.setLevel(logging.DEBUG)
9+
10+
handler = logging.StreamHandler(sys.stderr)
11+
logger.addHandler(handler)
12+
13+
logging.getLogger('selenium.webdriver.remote').setLevel(logging.WARN)
14+
logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG)
15+
16+
async def run(driver):
17+
async with driver.bidi_connection() as session:
18+
await trio.to_thread.run_sync(lambda: driver.get('https://blg.tch.re'))
19+
20+
links = driver.find_elements(By.TAG_NAME, 'a')
21+
22+
for a in links:
23+
print(a.get_attribute("href"))
24+
25+
options = webdriver.ChromeOptions()
26+
options.page_load_strategy = 'normal'
27+
options.enable_bidi = True
28+
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
29+
30+
driver = webdriver.Chrome(options)
31+
32+
trio.run(run, driver)

0 commit comments

Comments
 (0)