npm.io
1.2.3 • Published 1 year ago

safelinkify

Licence
MIT
Version
1.2.3
Deps
7
Size
2.1 MB
Vulns
0
Weekly
0
Stars
3

Safelinkify

npm version www.webmanajemen.com LICENSE GitHub language count Github Workflow GitHub forks GitHub stars

Customized safelink url redirector. Transform and Anonymize all hyperlinks to outbound pages. Useful for SEO external links and ADS.

Demo

page source samples
/page/safelink.html safelink-decode.js
layout
template
compiler
/page/safelink.html?url=aHR0cHM6Ly...

Installation

Bundles
registry link commands
npm https://www.npmjs.com/package/safelinkify npm i safelinkify -D
github https://github.com/dimaslanjaka/safelink npm i https://github.com/dimaslanjaka/safelink -D
tarball https://github.com/dimaslanjaka/safelink/raw/master/release/safelinkify.tgz npm i https://github.com/dimaslanjaka/safelink/raw/master/release/safelinkify.tgz -D
npm
npm install safelinkify -D
Using yarn
yarn add safelinkify --dev

Development

git clone --single-branch --branch main https://github.com/dimaslanjaka/safelink foldername
cd foldername
yarn install # or npm install
Command Description
yarn start Serve generated docs
yarn dev Watch and build docs
yarn run docs Build docs
yarn run build Build dist

Usage

Options
const options = {
  // Exclude patterns (do not anonymize these patterns)
  exclude: [
    'domain.com',
    /another.domain.com/,
    /https?:\/\/?([^*]+)\.)?webmanajemen\.com/,
    /([a-z0-9](?:[a-z0-9-]{1,61}[a-z0-9])?[.])*webmanajemen\.com/
  ],
  // URL redirector
  redirect: 'https://www.webmanajemen.com/page/safelink.html?url=',
  // Debug
  verbose: false,
  // Encryption type: 'base64' | 'aes'
  type: 'base64',
  // Password for AES (default: 'root')
  password: 'unique-password'
};
Browser

Script location: node_modules/safelinkify/dist/bundle.min.js

Include the script:

<script src="dist/bundle.min.js"></script>
<!-- or use CDN -->
<script src="https://raw.githack.com/dimaslanjaka/safelink/main/dist/bundle.min.js"></script>
<script src="https://cdn.statically.io/gh/dimaslanjaka/safelink/main/dist/bundle.min.js"></script>

Usage example:

<script>
  const sf = new safelink(options);
  // Automatically safelinkify all hyperlinks in body
  sf.parse(document.querySelector('body')).then((result) => {
    console.log(result);
    // In-page redirector
    sf.resolveQueryUrl(window.location.href);
  });
</script>
Node.js
Modern framework (vite,webpack,etc)
import * as safelink from 'safelinkify/browser_module';

const sf = new safelinkify.safelink(options);
const processedExternalLinks = sf.parse(`
<a href="www.example.com/page.php?id=xxxx&name=yyyy">external</a>
<a href="http://www.example.com/page.php?id=xxxx&name=yyyy">external</a>
<a href="https://www.example.com/page.php?id=xxxx&name=yyyy">external</a>
<a href="www.example.com/page.php/404"></a>
<a href="http://external.domain.com">internal</a>
<a href="http://www.webmanajemen.com">internal</a>
<a href="http://webmanajemen.com">internal</a>
<a href="#http://webmanajemen.com">#internal</a>
<a href="?http://webmanajemen.com">?internal</a>
<a href="">internal</a>
`);
processedExternalLinks.then(console.log);
Reference Examples
Import
const { safelink } = require('safelinkify');
// or
const { default: safelink } = require('safelinkify/dist/safelink');
Usage Example
import safelinkify from 'safelinkify';
// const safelinkify = require('safelinkify');
const sf = new safelinkify.safelink(options);
const processedExternalLinks = sf.parse(`
<a href="www.example.com/page.php?id=xxxx&name=yyyy">external</a>
<a href="http://www.example.com/page.php?id=xxxx&name=yyyy">external</a>
<a href="https://www.example.com/page.php?id=xxxx&name=yyyy">external</a>
<a href="www.example.com/page.php/404"></a>
<a href="http://external.domain.com">internal</a>
<a href="http://www.webmanajemen.com">internal</a>
<a href="http://webmanajemen.com">internal</a>
<a href="#http://webmanajemen.com">#internal</a>
<a href="?http://webmanajemen.com">?internal</a>
<a href="">internal</a>
`);
processedExternalLinks.then(console.log);

Result:

<a href="www.example.com/page.php?id=xxxx&name=yyyy">external</a>
<a href="https://www.webmanajemen.com/page/safelink.html?url=aHR0cDovL3d3dy5leGFtcGxlLmNvbS9wYWdlLnBocD9pZD14eHh4Jm5hbWU9eXl5eQ==">external</a>
<a href="https://www.webmanajemen.com/page/safelink.html?url=aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vcGFnZS5waHA/aWQ9eHh4eCZuYW1lPXl5eXk=">external</a>
<a href="www.example.com/page.php/404"></a>
<a href="http://external.domain.com">internal</a>
<a href="http://www.webmanajemen.com">internal</a>
<a href="http://webmanajemen.com">internal</a>
<a href="#http://webmanajemen.com">#internal</a>
<a href="?http://webmanajemen.com">?internal</a>
<a href="">internal</a>
Using Gulp

Reference: Gulp safelink task

import gulp from 'gulp';
import sf from 'safelinkify';
import { toUnix, join } from 'upath';
import through2 from 'through2';

const destDir = join(__dirname, 'build');

gulp.task('safelink', () => {
  const safelink = new sf.safelink({
    exclude: [
      /https?:\/\/?([^*]+)\.)?webmanajemen\.com/,
      /([a-z0-9](?:[a-z0-9-]{1,61}[a-z0-9])?[.])*webmanajemen\.com/
    ],
    redirect: 'https://www.webmanajemen.com/page/safelink.html?url=',
    verbose: false,
    type: 'base64',
    password: 'unique-password'
  });
  return gulp
    .src(['**/*.html'], {
      cwd: destDir,
      ignore: [
        '**/tmp/**',
        '**/node_modules/**',
        '**/monsters/**/*',
        '**/attendants/**/*',
        '**/materials/**/*',
        '**/scenic-spots/**/*',
        '**/static/**/*'
      ]
    })
    .pipe(
      through2.obj(async (file, _enc, next) => {
        if (file.isNull()) return next();
        const content = String(file.contents);
        const parsed = await safelink.parse(content);
        if (parsed) {
          file.contents = Buffer.from(parsed);
          next(null, file);
        } else {
          console.log('cannot parse', toUnix(file.path).replace(toUnix(process.cwd()), ''));
          next();
        }
      })
    )
    .pipe(gulp.dest(destDir));
});

Keywords