Git Product home page Git Product logo

brickolicious / angular-url-preview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dimitrov-d/angular-url-preview

0.0 0.0 0.0 912 KB

A lightweight URL preview card UI component which shows the metadata (title, description, image and site name) of a custom URL

Home Page: https://www.npmjs.com/package/angular-url-preview

JavaScript 15.59% TypeScript 64.29% HTML 10.75% SCSS 9.37%

angular-url-preview's Introduction

Angular URL Preview

Angular UI card component which fetches and displays metadata from a URL (title, description, image and site name).

The latest stable Angular version this package is built with is 13.0.0

Angular URL Preview npm ย  Angular URL Preview npm

This package uses a proxy server to bypass CORS restriction and mimic a request with an "Access-Control-Allow-Origin" HTTP header present. It behaves similarly to CORS anywhere without the need for an opt-in or a URL whitelist/blacklist.

The Open Graph Protocol is used to scrape the metadata and gather the underlying URL information.

The proxy server also uses Supabase for caching the request responses to achieve instant subsequent metadata loading and Redis to create a request limiter in order to prevent server overloading.

Installation

npm install angular-url-preview

In app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { AngularUrlPreviewModule } from 'angular-url-preview';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, AngularUrlPreviewModule],
  bootstrap: [AppComponent]
})
export class AppModule { }

Usage

In the component's template:

<ngx-url-preview url="example.com"></ngx-url-preview>

Specifying the URL is mandatory and it can be in any format, such as:

Customization

displayImage: boolean - Specify whether the image should be shown in the card. Default is true.

customImageSrc: string - Source of the image used in the cards. Source URL can be relative e.g. /assets/image.jpg or an absolute url.

styledFooter: boolean - Specify whether the footer should be styled, which adds a background color and a top border. Default is false.

height: string | number - Height of the card. Can be set in multiple formats, such as for example "400" (string), 400 (number) or "400px". Same rule is valid for the options below.

width: string | number - Width of the card.

imageHeight: string | number - Height of the image in the card. The width of the image adapts to the width of the card.

Additionally, it is possible to specify global styles for the component, which will be valid for every instance of the component without having to repeatedly specify the same configuration. To achieve this, add the .forRoot(config: URLPreviewConfig) configuration to the import of the AngularUrlPreviewModule in your main module. The URLPreviewConfig is a subset of the AngularUrlPreviewComponent and optionally contains all of the fields above. Example:

@NgModule({
  ...
  imports: [
  ...
	AngularUrlPreviewModule.forRoot({ displayImage: false, styledFooter: true, height: 300 })
  ]
})

Development Server (Demo application)

To try out the demo app, in the root folder run npm install

After that, run ng serve or npm start for a development server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

The source files of the demo app are located in projects/url-preview-demo/

Troubleshooting

It is possible that an error occurs while attempting to scrape the metadata from a website. It's important to note that an error most usually occurs if the target resource is not found or it does not provide the adequate metadata to its client. To check if an error has occurred it's a good idea to take a look at the developer console of your browser.

404 Not found

If this error occurs then firstly verify that the URL you have supplied is accurate and that the webpage it points to exists. If it does and the error persists, then the webpage most likely contains anti-bot protection and does not allow metadata scraping through a proxy server.

Image not loaded

Some websites provide a relative source which points to their banner image in their metadata. This is wrong as it is consequently unable to be found from a different host. Note that this mistake is corrected by angular-url-preview.

If for a different reason the image is not loaded, it is not provided, the resource is forbidden, or it is unsuitable, you may supply a custom image source (either relative or absolute) to the library, which will display the custom image by priority.

Example:

<ngx-url-preview url="example.com" customImageSrc="https://custom.com/logo.png"></ngx-url-preview>

Important: If you are using a relative path for the custom image source, make sure the image is located in the assets folder of your application (most usually src/assets).

Afterwards, you need to specify the assets in angular.json:

...
"architect": {
	"build": {
		"options": {
			...
			"assets": [
				...
				"src/assets"
			],
			...

And then in the component's template:

<ngx-url-preview url="example.com" customImageSrc="/assets/source.jpg"></ngx-url-preview>

Other metadata missing

You should note that this is completely up to the webpage, which has missing or unspecified metadata which was not able to be gathered through the open graph protocol. There are as many defaults and fallbacks as possible handled by angular-url-preview itself.

Slow metadata loading

It is possible that the loading of a website metadata is slow. Note that this will most likely occur only on the first load of the page. Afterwards, a Supabase cache is used to cache the metadata, which will in turn quickly load the metadata on subsequent requests.

angular-url-preview's People

Contributors

dimitrov-d avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.