How can I call Web APIs in React JSX? - javascript

I'm writing a music player with React and Electron and want to add metadata for the audio so as I can get MPRIS support. So I did some searching and found Media Session API is what I needed.
To test it, I copied the code from the exmaple code contained by the above link into a function in my project.
navigator.mediaSession.metadata = new MediaMetadata({
title: 'Unforgettable',
artist: 'Nat King Cole',
album: 'The Ultimate Collection (Remastered)',
artwork: [
{ src: 'https://dummyimage.com/96x96', sizes: '96x96', type: 'image/png' },
{ src: 'https://dummyimage.com/128x128', sizes: '128x128', type: 'image/png' },
{ src: 'https://dummyimage.com/192x192', sizes: '192x192', type: 'image/png' },
{ src: 'https://dummyimage.com/256x256', sizes: '256x256', type: 'image/png' },
{ src: 'https://dummyimage.com/384x384', sizes: '384x384', type: 'image/png' },
{ src: 'https://dummyimage.com/512x512', sizes: '512x512', type: 'image/png' },
]
});
I put it into a function that a component will call when the next song is played.However, I got errors like this:
Failed to compile.
./src/components/FM/Cover/index.jsx
Line 112: 'MediaMetadata' is not defined no-undef
Search for the keywords to learn more about each error.
Why?

You need to reference the window object.
window.MediaMetadata
^^^^^^
This is because babel/es-lint does not know MediaMetadata exists and will throw an error. Since Chrome injects this object as a global variable, it is accessible via the window object.
(re-posting what #AngelSalazar said in the comments of the OP for ease of use)

Related

Strapi: Problem uploading images to strapi, image does not render

Problem
I would like to know if you can help identify the issue related to uploading an image to the thumb field belonging to the content-type Directory.
The image image does not render once it is in the strapi panel, it only shows the name.
const getFileSize = (filePath) => {
return statSync(filePath)["size"]; // statSync from `fs-extra` library
};
strapi.service("api::directory.directory").create({
data: {},
files: {
thumb: {
path: `${imagesDir}/Crab-1-1-scaled.jpg`,
name: "Crab-1-1-scaled.jpeg",
type: mime.lookup(`${imagesDir}/Crab-1-1-scaled.jpg`), // mime from `mime-types` library
size: getFileSize(`${imagesDir}/Crab-1-1-scaled.jpg`),
},
},
});
console.log
size is always zero
{
files: {
thumb: {
path: 'src/scripts/Directory/images/Crab-1-1-scaled.jpg',
name: 'Crab-1-1-scaled.jpeg',
type: 'image/jpeg',
size: 0
}
}
}
output
When uploading the data the image is not rendered, it only shows the name of the image.

Deleting images in Imgbb via API

i'm using the imgbb API since it is very straghtforward to use and quite cheap. The problem is that the documentation is very poor and it does not explain how to delete an image through the API. I want to be able to delete the photo from my server in the case something happens (ex. if I delete a user).
My server is written in node.js.
pd. Also the response object I get after uploading an image is this one:
{
data: {
id: 'c3VRs4x',
title: 'client1-Bali',
url_viewer: 'https://ibb.co/c3VRs4x',
url: 'https://i.ibb.co/Pj0JVqt/client1-Bali.jpg',
display_url: 'https://i.ibb.co/1QjB4Fb/client1-Bali.jpg',
size: 60385,
time: '1594835546',
expiration: '0',
image: {
filename: 'client1-Bali.jpg',
name: 'client1-Bali',
mime: 'image/jpeg',
extension: 'jpg',
url: 'https://i.ibb.co/Pj0JVqt/client1-Bali.jpg'
},
thumb: {
filename: 'client1-Bali.jpg',
name: 'client1-Bali',
mime: 'image/jpeg',
extension: 'jpg',
url: 'https://i.ibb.co/c3VRs4x/client1-Bali.jpg'
},
medium: {
filename: 'client1-Bali.jpg',
name: 'client1-Bali',
mime: 'image/jpeg',
extension: 'jpg',
url: 'https://i.ibb.co/1QjB4Fb/client1-Bali.jpg'
},
delete_url: 'https://ibb.co/c3VRs4x/b3072de2f5287a39f81c7dec3cd8a236'
},
success: true,
status: 200
}
Edit: Here is the link to the imgbb API documentation: https://api.imgbb.com/
Imgbb does not allow you to delete images. I just tried going to the delete URL to delete the image and the image got deleted on that page but not on the other links in the JSON. If the functionality is not implemented on the site itself there is no use trying on the API. If they did have the functionality to entirely delete an image then you could have used web scraping to press the buttons.

How to set the HTML video title that is shown in Chrome and Windows?

How can I set the name (label) of HTML5 <video> element that browsers (like Chrome) and Windows OS display?
Like the title that Windows shows for this youtube video (when changing speaker volume):
Currently for me the page title is shown (the title set in page head).
I tried adding a title="..." attribute to the video element but it had no effect. Also didn't find a solution on StackOverflow.
I'm not sure for the video. But in some browsers, mediaSession worked for me. Javascript...
if ('mediaSession' in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: 'name',
artist: '',
album: '',
artwork: [{ src: '...' }]
});
}
As the other answer stated, it can be done with JavaScript and Media Session API.
Check out this YouTube video.
if ("mediaSession" in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: "Unforgettable",
artist: "Nat King Cole",
album: "The Ultimate Collection (Remastered)",
artwork: [
{ src: "https://dummyimage.com/128x128", sizes: "128x128", type: "image/png" },
{ src: "https://dummyimage.com/512x512", sizes: "512x512", type: "image/png" }
]
});
}

How add adsense to flowplayer?

I have next problem, flowplayer have next documentation:
<div id="image_text" class="flowplayer functional"></div>
<!-- install the player with ima AdSense configuration -->
<script>
flowplayer("#image_text", {
// configure the AdSense plugin for this player
ima: {
// must be set when testing your installation
adtest: true,
// make the Google robots crawl the current page
description_url: location.href,
// adverts configuration
ads: [{
// mandatory: schedule ad time
// here: 3 seconds into the video
time: 3,
// request an advert of type image or text type
ad_type: "image_text"
}]
},
splash: true,
ratio: 5/12,
rtmp: "rtmp://s3b78u0kbtx79q.cloudfront.net/cfx/st",
clip: {
sources: [
{ type: "video/webm",
src: "//stream.flowplayer.org/bauhaus.webm" },
{ type: "video/mp4",
src: "//stream.flowplayer.org/bauhaus.mp4" },
{ type: "video/flash", src: "mp4:bauhaus" }
]
}
});
</script>
We add empty block for adsense and some script for this block write? But where i need to write adcode? Please help me, how do I add Google ads to flowplayer, sorry for worst english.

Save multiple models in loopback

I'm doing research on loopback and was wondering if it's possible to save to multiple models from one request. Say.. an Post has many Tags with many Images. A user form would have the following:
Post Title
Post Description
Tag Names (A multi field. E.g.: ['Sci-Fi', 'Fiction', 'BestSeller']
Image File (Hoping to process the file uploaded to AWS, maybe with skipper-s3?)
How would I be able to persist on multiple models like this? Is this something you do with a hook?
You can create RemoteMethods in a Model, which can define parameters, so in your example you could create something like this in your Post model:
// remote method, defined below
Post.SaveFull = function(title,
description,
tags,
imageUrl,
cb) {
var models = Post.app.Models; // provides access to your other models, like 'Tags'
Post.create({"title": title, "description": description}, function(createdPost) {
foreach(tag in tags) {
// do something with models.Tags
}
// do something with the image
// callback at the end
cb(null, {}); // whatever you want to return
})
}
Post.remoteMethod(
'SaveFull',
{
accepts: [
{arg: 'title', type: 'string'},
{arg: 'description', type: 'string'},
{arg: 'tags', type: 'object'},
{arg: 'imageUrl', type: 'string'}
],
returns: {arg: 'Post', type: 'object'}
}
);

Categories

Resources