Code Monkey home page Code Monkey logo

metaretriver.js's Introduction

MetaRetriver.js

A small library for extracting data from meta tags. This library can also be used to design link previews.

How To Use It ?

Get MetaRetriver.js Library Download Code

   <script src="MetaRetriver.js"></script>

then

// Example Code

let url="https://api.allorigins.win/raw?url=https://www.animenewsnetwork.com/cms/.163738";

//specifying meta tags by property and name to get its content
let metaTags=["property=og:image","property=og:url","name=description"];

MetaRetriver(url).retrive(true,metaTags,function(data){

    console.log(data.get("property=og:image"));
    console.log(data.get("name=description"));
});

The library will get the meta tags contents from the webpage. Due to CORS issue I used All Origins to pull contents from page.

Cached Results

The library caches the whole html page in HTML Web Storage API by default. If you don't want to cache html results then you can turn it off and use this code instead

// Example Code

let url="https://api.allorigins.win/raw?url=https://www.animenewsnetwork.com/cms/.163738";

//specifying meta tags by property and name to get its content
let metaTags=["property=og:image","property=og:url","name=description"];

//Passing "false" as we don't want caching
MetaRetriver(url).retrive(false,metaTags,function(data){

    console.log(data.get("property=og:image"));
    console.log(data.get("name=description"));
});

the cached data can be accessed using this code

// Example Code
let url="https://api.allorigins.win/raw?url=https://www.animenewsnetwork.com/cms/.163738";

console.log(localStorage.getItem(url));

Designing Link Previews using MetaRetriver.js

Example Layout using Bootstrap and code

HTML

   
    <div class="card" style="width: 18rem;">
            <img id="image" src="..." class="card-img-top" alt="...">
            <div class="card-body">
              <h5 class="card-title" id="title"></h5>
              <p class="card-text" id="desc"></p>
            </div>
          </div>

JavaScript

let url="https://api.allorigins.win/raw?url=https://www.animenewsnetwork.com/cms/.163948";
let metaTags=["property=og:image","property=og:title","name=description"];
MetaRetriver(url).retrive(true,metaTags,function(data){

    document.getElementById("image").setAttribute("src",data.get("property=og:image"));
    document.getElementById("desc").innerHTML=data.get("name=description");
    document.getElementById("title").innerHTML=data.get("property=og:title");
    
});

Output

Example

metaretriver.js's People

Contributors

tuduabhishek avatar

Stargazers

 avatar

Watchers

 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.