openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

trumbowyg.pasteembed.js (4181B)


      1 /* ===========================================================
      2  * trumbowyg.pasteembed.js v1.0
      3  * Url paste to iframe with noembed. Plugin for Trumbowyg
      4  * http://alex-d.github.com/Trumbowyg
      5  * ===========================================================
      6  * Author : Max Seelig
      7  *          Facebook : https://facebook.com/maxse
      8  *          Website : https://www.maxmade.nl/
      9  */
     10 
     11 (function($) {
     12     'use strict';
     13 
     14     var defaultOptions = {
     15         enabled: true,
     16         endpoints: [
     17             'https://noembed.com/embed?nowrap=on',
     18             'https://api.maxmade.nl/url2iframe/embed'
     19         ]
     20     };
     21 
     22     $.extend(true, $.trumbowyg, {
     23         plugins: {
     24             pasteEmbed: {
     25                 init: function(trumbowyg) {
     26                     trumbowyg.o.plugins.pasteEmbed = $.extend(true, {}, defaultOptions, trumbowyg.o.plugins.pasteEmbed || {});
     27 
     28                     if (!trumbowyg.o.plugins.pasteEmbed.enabled) {
     29                         return;
     30                     }
     31 
     32                     trumbowyg.pasteHandlers.push(function(pasteEvent) {
     33                         try {
     34                             var clipboardData = (pasteEvent.originalEvent || pasteEvent).clipboardData,
     35                                 pastedData = clipboardData.getData('Text'),
     36                                 endpoints = trumbowyg.o.plugins.pasteEmbed.endpoints,
     37                                 request = null;
     38 
     39                             if (pastedData.startsWith('http')) {
     40                                 pasteEvent.stopPropagation();
     41                                 pasteEvent.preventDefault();
     42 
     43                                 var query = {
     44                                     url: pastedData.trim()
     45                                 };
     46                                 var content = '';
     47                                 var index = 0;
     48 
     49                                 if (request && request.transport) {
     50                                     request.transport.abort();
     51                                 }
     52 
     53                                 request = $.ajax({
     54                                     crossOrigin: true,
     55                                     url: endpoints[index],
     56                                     type: 'GET',
     57                                     data: query,
     58                                     cache: false,
     59                                     dataType: 'jsonp',
     60                                     success: function(res) {
     61                                         if (res.html) {
     62                                             index = 0;
     63                                             content = res.html;
     64                                         } else {
     65                                             index += 1;
     66                                         }
     67                                     },
     68                                     error: function() {
     69                                         index += 1;
     70                                     },
     71                                     complete: function() {
     72                                         if (content.length === 0 && index < endpoints.length - 1) {
     73                                             this.url = endpoints[index];
     74                                             this.data = query;
     75                                             $.ajax(this);
     76                                         }
     77                                         if (index === endpoints.length - 1) {
     78                                             content = $('<a>', {
     79                                                 href: pastedData,
     80                                                 text: pastedData
     81                                             }).prop('outerHTML');
     82                                         }
     83                                         if (content.length > 0) {
     84                                             index = 0;
     85                                             trumbowyg.execCmd('insertHTML', content);
     86                                         }
     87                                     }
     88                                 });
     89                             }
     90                         } catch (c) {}
     91                     });
     92                 }
     93             }
     94         }
     95     });
     96 })(jQuery);