-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
473 additions
and
100 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 was deleted.
Oops, something went wrong.
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,61 @@ | ||
import { getBrowserInstance } from '../../utilities/getBrowserInstance.js'; | ||
import { stdResponse, invalidRequest, stdPostBody } from "../../utilities/requestHelpers.js"; | ||
|
||
export default async function handler(req, res) { | ||
const body = stdPostBody(req); | ||
const urlToCapture = body.urlToCapture; | ||
// Perform URL validation | ||
if (!urlToCapture || !urlToCapture.trim()) { | ||
res = invalidRequest(res, 'enter a valid url'); | ||
} | ||
else { | ||
if (!urlToCapture.includes("https://")) { | ||
// try to fake it | ||
urlToCapture = `https://${urlToCapture}`; | ||
} | ||
|
||
// capture options | ||
var browserGoToOptions = { | ||
timeout: 10000, | ||
waitUntil: 'networkidle2', | ||
}; | ||
var screenshotOptions = { | ||
quality: body.quality ? parseInt(body.quality) : 75, | ||
type: 'jpeg', | ||
encoding: "base64" | ||
}; | ||
var base64 = ''; | ||
let browser = null | ||
try { | ||
browser = await getBrowserInstance(); | ||
let page = await browser.newPage(); | ||
await page.goto(urlToCapture, browserGoToOptions); | ||
// special support for isolating a tweet | ||
if (urlToCapture.includes('twitter.com')) { | ||
await page.waitForSelector("article[data-testid='tweet']"); | ||
const element = await page.$("article[data-testid='tweet']"); | ||
base64 = await element.screenshot(screenshotOptions); | ||
} | ||
else { | ||
screenshotOptions.fullPage = true; | ||
base64 = await page.screenshot(screenshotOptions); | ||
} | ||
res = stdResponse(res, | ||
{ | ||
url: urlToCapture, | ||
image: base64 | ||
}, { | ||
methods: "GET,OPTIONS,PATCH,DELETE,POST,PUT", | ||
cache: 1800 | ||
} | ||
); | ||
} catch (error) { | ||
console.log(error) | ||
res = invalidRequest(res, 'something went wrong', 500); | ||
} finally { | ||
if (browser !== null) { | ||
await browser.close() | ||
} | ||
} | ||
} | ||
} |
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,131 @@ | ||
import { LitElement, css, html } from "lit"; | ||
import { MicroFrontendRegistry } from "../micro-frontend-registry.js"; | ||
import { enableServices } from '../lib/microServices.js'; | ||
|
||
export class DocxExample extends LitElement { | ||
static get tag() { | ||
return "docx-example"; | ||
} | ||
static get styles() { | ||
return [ | ||
css` | ||
:host { | ||
display: block; | ||
} | ||
textarea { | ||
display: block; | ||
} | ||
#asciihere { | ||
background-color: black; | ||
font-family: monospace; | ||
white-space: pre; | ||
} | ||
`, | ||
]; | ||
} | ||
constructor() { | ||
super(); | ||
// enable these services | ||
enableServices(['core']); | ||
} | ||
|
||
// ascii to image | ||
asciiImgRender() { | ||
return html` | ||
<label>Upload image file to get ASCII art</label> | ||
<input type="file" id="ascii" @change="${this.asciiImgUpload}" /> | ||
<textarea id="asciiresponse" rows="10" cols="50"></textarea> | ||
<h1>Ascii that was in the file upload</h1> | ||
<pre id="asciihere"></pre> | ||
`; | ||
} | ||
|
||
asciiImgUpload(event) { | ||
const files = event.target.files; | ||
const formData = new FormData(); | ||
formData.append('upload', files[0]); | ||
MicroFrontendRegistry.call('@core/imgToAscii', formData, this.toAsciiResponse.bind(this)); | ||
} | ||
|
||
toAsciiResponse(data) { | ||
this.shadowRoot.querySelector("#asciihere").innerHTML = data.data.image; | ||
this.shadowRoot.querySelector("#asciiresponse").value = JSON.stringify(data); | ||
} | ||
|
||
// docx to html | ||
docxToHtmlRender() { | ||
return html` | ||
<label>Upload Docx file to get HTML</label> | ||
<input type="file" id="upload" @change="${this.docxToHtmlUpload}" /> | ||
<textarea id="response" rows="10" cols="50"></textarea> | ||
<h1>HTML that was in the file upload</h1> | ||
<div id="here"></div> | ||
`; | ||
} | ||
|
||
docxToHtmlUpload(event) { | ||
const files = event.target.files; | ||
const formData = new FormData(); | ||
formData.append('upload', files[0]); | ||
MicroFrontendRegistry.call('@core/docxToHtml', formData, this.docxToHtmlResponse.bind(this)); | ||
} | ||
|
||
docxToHtmlResponse(data) { | ||
console.log(data); | ||
this.shadowRoot.querySelector("#here").innerHTML = data.data.contents; | ||
} | ||
|
||
// docx to pdf file | ||
docxToPdfRender() { | ||
return html` | ||
<label>Docx file to get PDF</label> | ||
<input type="file" id="uploadpdf" @change="${this.docxToPdfUpload.bind(this)}" /> | ||
<h1>PDF file</h1> | ||
<iframe id="pdfframe" width="100%" height="500px"></iframe> | ||
`; | ||
} | ||
|
||
docxToPdfUpload(event) { | ||
const files = event.target.files; | ||
const formData = new FormData(); | ||
formData.append('upload', files[0]); | ||
MicroFrontendRegistry.call('@core/docxToPdf', formData, this.docxToPdfResponse.bind(this)); | ||
} | ||
|
||
docxToPdfResponse(data) { | ||
console.log(data); | ||
this.shadowRoot.querySelector("#pdfframe").src = `data:application/pdf;base64,${data.data.pdf}`; | ||
this.shadowRoot.querySelector("#response").value = JSON.stringify(data); | ||
} | ||
|
||
screenshotUrlRender() { | ||
return html` | ||
<label>Enter a URL to get a screenshot of it</label> | ||
<input type="text" id="screenshot" /> | ||
<button id="getthething" @click="${this.screenshotUrlLink}">Get a screenshot</button> | ||
<h1>Image will appear here</h1> | ||
<img id="replaceimg" src="" alt="" /> | ||
`; | ||
} | ||
|
||
screenshotUrlLink(event) { | ||
const urlToCapture = this.shadowRoot.querySelector('#screenshot').value; | ||
MicroFrontendRegistry.call('@core/screenshotUrl', {urlToCapture: urlToCapture, quality: 80 }, this.screenshotUrlResponse.bind(this)); | ||
} | ||
|
||
screenshotUrlResponse(data) { | ||
this.shadowRoot.querySelector("#replaceimg").src = `data:image/jpeg;base64, ${data.data.image}`; | ||
this.shadowRoot.querySelector("#replaceimg").alt = `screenshot of ${data.data.url}`; | ||
} | ||
|
||
render() { | ||
return html` | ||
${this.asciiImgRender()} | ||
${this.docxToHtmlRender()} | ||
${this.docxToPdfRender()} | ||
${this.screenshotUrlRender()} | ||
`; | ||
} | ||
} | ||
|
||
customElements.define(DocxExample.tag, DocxExample); |
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
Oops, something went wrong.
6cb1dea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
lrnwebcomponents – ./
lrnwebcomponents-elmsln.vercel.app
lrnwebcomponents.vercel.app
lrnwebcomponents-git-master-elmsln.vercel.app