Skip to content

Instantly share code, notes, and snippets.

@emreavcilar
Last active June 12, 2022 18:01
Show Gist options
  • Save emreavcilar/8edcd18f76f21e36e58f1e39c2e915cf to your computer and use it in GitHub Desktop.
Save emreavcilar/8edcd18f76f21e36e58f1e39c2e915cf to your computer and use it in GitHub Desktop.
/*
browser functions
*/
/*
javascript to connect a bluetooth device. Displays a popup dialog to pair a device with the
current web app and then you can communicate with that device in your #reactjs app.
*/
/*
#javascript to connect a #bluetooth device.
Displays a popup dialog to pair a device with
the current web app and then you can communicate
with that device in your #reactjs app.
Give it a try in the console!
Works only on Chromium based web browsers.
*/
navigator
.bluetooth
.requestDevice({acceptAllDevices:true})
.then(console.log);
/*
๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜ ๐˜๐—ถ๐—ฝ: Check Network Status
How to detect if the internet connection is online or offline in JavaScript.
Also, how to listen for a change in the network status.
*/
//listen for a change in the network status
window.addEventListener('offline', (e) => {
console.log('You are offline');
});
window.addEventListener('online', (e) => {
console.log('You are online');
});
// get current status
if(navigator.online){
console.log('online');
} else {
console.log('offline')
}
/*
JavaScript Tip ๐Ÿ’ก
You can use the "navigator.clipboard.writeText" function to copy text to clipboard.
It's asynchronous - and you don't have to artificially place it in the DOM first (like we had to in the old days).
*/
https://www.linkedin.com/posts/simonhoiberg_javascript-tip-you-can-use-the-navigatorclipboardwritetext-activity-6924264860713156608-SQCH?utm_source=linkedin_share&utm_medium=member_desktop_web
/*WRONG !!!!!!!*/
document.execCommand('copy')
/*TRUE !!!!!*/
navigator.clipboard.writeText('npm i twitter-api-client');
/*
๐—ป๐—ฎ๐˜ƒ๐—ถ๐—ด๐—ฎ๐˜๐—ผ๐—ฟ.๐—ด๐—ฒ๐˜๐—•๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐˜†() ๐˜ฎ๐˜ฆ๐˜ต๐˜ฉ๐˜ฐ๐˜ฅ ๐˜ฑ๐˜ณ๐˜ฐ๐˜ท๐˜ช๐˜ฅ๐˜ฆ๐˜ด ๐˜ช๐˜ฏ๐˜ง๐˜ฐ๐˜ณ๐˜ฎ๐˜ข๐˜ต๐˜ช๐˜ฐ๐˜ฏ ๐˜ข๐˜ฃ๐˜ฐ๐˜ถ๐˜ต ๐˜ต๐˜ฉ๐˜ฆ ๐˜ด๐˜บ๐˜ด๐˜ต๐˜ฆ๐˜ฎ'๐˜ด ๐˜ฃ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜บ
- ๐˜ถ๐˜ด๐˜ฆ๐˜ฅ ๐˜ต๐˜ฐ ๐˜ช๐˜ฎ๐˜ฑ๐˜ญ๐˜ฆ๐˜ฎ๐˜ฆ๐˜ฏ๐˜ต ๐˜ฃ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜บ-๐˜ด๐˜ข๐˜ท๐˜ช๐˜ฏ๐˜จ ๐˜ง๐˜ถ๐˜ฏ๐˜ค๐˜ต๐˜ช๐˜ฐ๐˜ฏ๐˜ข๐˜ญ๐˜ช๐˜ต๐˜ช๐˜ฆ๐˜ด ๐˜ธ๐˜ฉ๐˜ฆ๐˜ฏ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ถ๐˜ด๐˜ฆ๐˜ณ'๐˜ด ๐˜ฃ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜บ ๐˜ช๐˜ด ๐˜ญ๐˜ฐ๐˜ธ,
๐˜ญ๐˜ช๐˜ฌ๐˜ฆ ๐˜ฌ๐˜ช๐˜ญ๐˜ญ๐˜ช๐˜ฏ๐˜จ ๐˜ด๐˜ฐ๐˜ฎ๐˜ฆ ๐˜ฃ๐˜ข๐˜ค๐˜ฌ๐˜จ๐˜ณ๐˜ฐ๐˜ถ๐˜ฏ๐˜ฅ ๐˜ฑ๐˜ณ๐˜ฐ๐˜ค๐˜ฆ๐˜ด๐˜ด๐˜ฆ๐˜ด ๐˜ต๐˜ฉ๐˜ข๐˜ต ๐˜ฅ๐˜ณ๐˜ข๐˜ช๐˜ฏ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฃ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜บ ๐˜ง๐˜ข๐˜ด๐˜ต๐˜ฆ๐˜ณ
- ๐˜๐˜ต ๐˜ณ๐˜ฆ๐˜ต๐˜ถ๐˜ณ๐˜ฏ๐˜ด ๐˜ข ๐˜ฃ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜บ ๐˜ฑ๐˜ณ๐˜ฐ๐˜ฎ๐˜ช๐˜ด๐˜ฆ, ๐˜ธ๐˜ฉ๐˜ช๐˜ค๐˜ฉ ๐˜ช๐˜ด ๐˜ณ๐˜ฆ๐˜ด๐˜ฐ๐˜ญ๐˜ท๐˜ฆ๐˜ฅ ๐˜ช๐˜ฏ ๐˜ข ๐˜‰๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜บ๐˜”๐˜ข๐˜ฏ๐˜ข๐˜จ๐˜ฆ๐˜ณ ๐˜ฐ๐˜ฃ๐˜ซ๐˜ฆ๐˜ค๐˜ต ๐˜ฑ๐˜ณ๐˜ฐ๐˜ท๐˜ช๐˜ฅ๐˜ช๐˜ฏ๐˜จ
๐˜ข๐˜ญ๐˜ด๐˜ฐ ๐˜ด๐˜ฐ๐˜ฎ๐˜ฆ ๐˜ฏ๐˜ฆ๐˜ธ ๐˜ฆ๐˜ท๐˜ฆ๐˜ฏ๐˜ต๐˜ด ๐˜บ๐˜ฐ๐˜ถ ๐˜ค๐˜ข๐˜ฏ ๐˜ฉ๐˜ข๐˜ฏ๐˜ฅ๐˜ญ๐˜ฆ ๐˜ต๐˜ฐ ๐˜ฎ๐˜ฐ๐˜ฏ๐˜ช๐˜ต๐˜ฐ๐˜ณ ๐˜ต๐˜ฉ๐˜ฆ ๐˜ฃ๐˜ข๐˜ต๐˜ต๐˜ฆ๐˜ณ๐˜บ ๐˜ด๐˜ต๐˜ข๐˜ต๐˜ถ๐˜ด.
*/
navigator.getBattery().then(
(b)=>
{
console.log(b)
});
/*
charging: true
chargingTime: 0
dischargingTime: Infinity
level: 1
onchargingchange: null
onchargingtimechange: null
ondischargingtimechange: null
onlevelchange: null
*/
navigator.getBattery().then((battery) => {
console.log('Battery Charging?', battery.charging);
console.log('Battery Level', battery.level);
console.log('Battery Discharging Time', battery.dischargingTime);
// Event Listeners Available
battery.addEventListener('chargingchange', () => {});
battery.addEventListener('levelchange', () => {})
battery.addEventListener('chargingtimechange', () => {})
battery.addEventListener('dischargingtimechange', () => {})
});
/*get user geolocation*/
navigator.geolocation.getCurrentPosition(
res => {
// success
const latitude = res.coords.latitude;
const longitude = res.coords.longitude;
},
error => {
// error
const code = error.code;
const message = error.message;
}
/*
detects user
*/
var isMobile =
{
android: function()
{
return navigator.userAgent.match(/Android/i);
},
blackBerry: function()
{
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function()
{
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
opera: function()
{
return navigator.userAgent.match(/Opera Mini/i);
},
windows: function()
{
return navigator.userAgent.match(/IEMobile/i);
},
any: function()
{
return (isMobile.android() || isMobile.blackBerry() || isMobile.iOS() || isMobile.opera() || isMobile.windows());
}
};
/*
#javascript to turn on the users webcam and show a live video.
*/
// HTML
<video autoplay="true" id="video"></video>
//Javascript
const el = document.getElementById("video");
navigator.mediaDevices
.getUserMedia({ video:true })
.then((stream) => el.srcObject = stream);
/*
javascript that sends data to the server when the tab is closed or the user navigates to another website.
*/
/*
Unlike a XMLHTTPRequest which can be cancelled when the page is unloaded.
The "sendBeacon" API ensures the request is sent to the server without being
interrupted.
*/
document.addEventListener('visibilitychange', ()=>{
if(document.visibilityState === 'hidden'){
navigator.sendBeacon('/log', data);
}
})
/*
#javascript to trigger the vibrator feedback on a mobile device.
*/
navigator.vibrate(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment