Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #49 from schoenbergerb/48-noscrape-from-arraybuffer
Browse files Browse the repository at this point in the history
Initialize Noscrape with ArrayBuffer
  • Loading branch information
schoenbergerb authored Feb 1, 2024
2 parents e0a8399 + ac11e36 commit e833b68
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
30 changes: 27 additions & 3 deletions example/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ app.get('/', (req, res) => {
</style>
</head>
<body class="">
<div style="width: 270px; height: 150px; overflow: hidden;">
<table id="original">
<tr>
Expand Down Expand Up @@ -99,10 +99,34 @@ app.get('/', (req, res) => {
`)
})

app.get('/remote-demo', async (_, res) => {
const response = await fetch('http://localhost:1337/example.ttf')
const buffer = await response.arrayBuffer();

const noscrape = new Noscrape(buffer);

const text = noscrape.obfuscate("this is another demo");

app.listen(1337, () => {
console.log('listen on port', 1337)

res.send(`<html lang="en">
<head>
<title>Noscrape - DEMO</title>
<style>
@font-face {
font-family: 'noscrape-obfuscated';
src: url('data:font/truetype;charset=utf-8;base64,${noscrape.getFont().toString('base64')}');
}
</style>
</head>
<body style="height: 100%; display: flex; justify-content: center; align-items: center;">
<div style="font-family: 'noscrape-obfuscated'">${text}</div>
</body>
</html>
`)
})


app.listen(1337)



18 changes: 14 additions & 4 deletions src/noscrape.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Font, Glyph, loadSync } from "opentype.js";
import { Font, Glyph, loadSync, parse } from "opentype.js";
import { DEFAULT_OPTIONS, ObfuscationOptions } from "./obfuscation-options";
import { value2glyphs } from "./value2glyphs";
import { obfuscateValue } from "./obfuscate/value";
Expand All @@ -23,16 +23,26 @@ export class Noscrape {
/**
* Initializes a new instance of the Noscrape class.
*
* @param {string} fontFilePath - The file path to the true-type font.
* @param {string | ArrayBuffer | SharedArrayBuffer} font
* In case of string: only local file path is provided. Otherwise, load file with fetch or similar api and provide
* (Shared)ArrayBuffer to Noscrape. Working example can be found at the Demo-Server sources:
* npm run demo -> http://localhost:1337/remote-demo
* @param {ObfuscationOptions} options - Optional configuration options for obfuscation.
*/
constructor(fontFilePath: string, options?: ObfuscationOptions) {
constructor(
font: string | ArrayBuffer | SharedArrayBuffer,
options?: ObfuscationOptions,
) {
this.options = {
...DEFAULT_OPTIONS,
...options,
};

this.font = loadSync(fontFilePath, { lowMemory: this.options.lowMemory });
if (typeof font === "string") {
this.font = loadSync(font, { lowMemory: this.options.lowMemory });
} else {
this.font = parse(font);
}
}

/**
Expand Down

0 comments on commit e833b68

Please sign in to comment.