Showing posts with label frontend. Show all posts
Showing posts with label frontend. Show all posts

Switch "ON" Android phone flashlight from web



Hey, have you ever think of switching ON an android phone flashlight from the web? Here is a source code for that below.

1. Open Your IDE



2. Create an HTML file


3. paste the following code:


 <button class="switch">On / Off</button>


4. Add Javascript To your code:


Run the code on your browser and boom 💥,  it works. 

If you have any questions feel free to ask at the comment section. 



//Test browser support
const SUPPORTS_MEDIA_DEVICES = 'mediaDevices' in navigator;

if (SUPPORTS_MEDIA_DEVICES) {
  //Get the environment camera (usually the second one)
  navigator.mediaDevices.enumerateDevices().then(devices => {
  
    const cameras = devices.filter((device) => device.kind === 'videoinput');

    if (cameras.length === 0) {
      throw 'No camera found on this device.';
    }
    const camera = cameras[cameras.length - 1];

    // Create stream and get video track
    navigator.mediaDevices.getUserMedia({
      video: {
        deviceId: camera.deviceId,
        facingMode: ['user', 'environment'],
        height: {ideal: 1080},
        width: {ideal: 1920}
      }
    }).then(stream => {
      const track = stream.getVideoTracks()[0];

      //Create image capture object and get camera capabilities
      const imageCapture = new ImageCapture(track)
      const photoCapabilities = imageCapture.getPhotoCapabilities().then(() => {

        //todo: check if camera has a torch

        //let there be light!
        const btn = document.querySelector('.switch');
        btn.addEventListener('click', function(){
          track.applyConstraints({
            advanced: [{torch: true}]
          });
        });
      });
    });
  });
  
  //The light will be on as long the track exists
  
  
}

Send mail using javascript cdn (No server side required)



Have you ever wondered how you can send mail from front end using cdn?

Am going to show you how it works , with these few steps :
1.  firstly logon to https://www.smtpjs.com/

2. Open your IDE , e.g sublime text, vscode.. 

3. Include the script: 
<script src="https://smtpjs.com/v3/smtp.js"></script>
4. Send an email using:
Email.send({
  Host : "smtp.yourisp.com",
    Username : "username",
    Password : "password",
    To : 'them@website.com',
    From : "you@isp.com",
    Subject : "This is the subject",
    Body : "And this is the body"
}).then(
  message => alert(message)
);
5. Fill your smtp server details , boom 💥.

How to add security to this?

According to smtpjs.com, you can encrypt your SMTP Credentials, using the script below.
Email.send({
    SecureToken : "C973D7AD-F097-4B95-91F4-40ABC5567812",
    To : 'them@website.com',
    From : "you@isp.com",
    Subject : "This is the subject",
    Body : "And this is the body"
}).then(
  message => alert(message)
);
More features available at smtpjs.com check it out. Don't forget to share.