import WebSocket from 'ws' ; import fetch from 'node-fetch' ; import { AuthHostOrigin , buildAuth , buildAuthHeaders , getAuthCookies } from './auth' ; import { buildCookieString } from './connection' ; export interface PartialProblemResponse { category ? : string ; group ? : string ; categoryHuman ? : string ; count ? : number ; } export interface PlagiarismResult extends PartialProblemResponse { text : string ; hasPlagiarism : boolean ; } export function getPlagiarismHostOrigin ( ) { return { Host : 'capi.grammarly.com' , Origin : 'https://www.grammarly.com' } ; } author Stewart McGown
  export async function plagiarism ( text , agent ? ) { const { Host , Origin } = getPlagiarismHostOrigin ( ) ; const auth = await buildAuth ( { origin : Origin , host : 'www.grammarly.com' , authUrl : 'https://www.grammarly.com/plagiarism-checker' , agent } ) ; const results = await fetch ( 'https://capi.grammarly.com/api/check' , { method : 'POST' , headers : buildAuthHeaders ( buildCookieString ( getAuthCookies ( auth ) ) , auth . gnar_containerId , Origin , Host ) , body : text , agent } ) . then ( r => r . json ( ) ) ; const detected = results . find ( r => r . category === 'Plagiarism' || r . group === 'Plagiarism' ) || { count : 0 } ; return { ... detected , text , hasPlagiarism : ! ! detected . count } ; }