Skip to content

Commit

Permalink
Merge pull request #19 from projectblacklight/without-jquery
Browse files Browse the repository at this point in the history
Remove jquery dependency
  • Loading branch information
jcoyne authored Jan 8, 2025
2 parents aa25888 + 4584c94 commit 69f7a25
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
31 changes: 31 additions & 0 deletions app/assets/javascripts/blacklight_oembed/oembed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default function oEmbed(elements) {
if (!elements) return;

// Ensure elements is an array-like collection
elements = elements instanceof NodeList ? Array.from(elements) : [elements];

elements.forEach(function(embedViewer) {
const embedURL = embedViewer.dataset.embedUrl; // Get the embed URL from the data attribute

if (!embedURL) return;

// Fetch JSON data from the embed URL
fetch(embedURL)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data === null) {
return;
}
// Set the inner HTML of the element
embedViewer.innerHTML = data.html;
})
.catch(error => {
console.error('Error fetching embed data:', error);
});
});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//= require 'blacklight_oembed/jquery.oembed.js'
import oembed from 'blacklight_oembed/oembed'

Blacklight.onLoad(function() {
$('[data-embed-url]').oEmbed();
});
oembed(document.querySelectorAll('[data-embed-url]'));
});

0 comments on commit 69f7a25

Please sign in to comment.