Skip to content

Instantly share code, notes, and snippets.

View akshar-dave's full-sized avatar
🌻

Akshar Dave akshar-dave

🌻
View GitHub Profile
@akshar-dave
akshar-dave / useKeyboardMappings.jsx
Created November 6, 2023 11:13
Keyboard Shortcut mappings in React
const useKeyboardBindings = map => {
useEffect(() => {
const handlePress = e => {
const handler = map[e.key];
if (e.repeat) return;
if (typeof handler === 'function') {
handler();
}
};
@akshar-dave
akshar-dave / CacheAPIResults.js
Created November 6, 2023 10:44
Cache API Results
const loadData = async (url) => {
var data = localStorage.getItem(url);
if (data) { // check if expired
data = JSON.parse(data);
if (Date.now() - data.timestamp > 600000) { // 10mins
localStorage.removeItem(url);
data = null;
}
}
@akshar-dave
akshar-dave / BlockLinkedInPostsWithHashtag.js
Last active August 24, 2022 06:29
Block LinkedIn posts with a specific hashtag
let hashtag = "MIDJOURNEY";
const removePostsWithHashtag = (posts, hashtag) => {
let docs = posts;
docs.forEach((doc => {
let caption = doc.innerText.toUpperCase();
let hasHashtag = caption.indexOf(hashtag) !== -1
if(!hasHashtag) return;
let thisPost = doc.closest(".artdeco-card");
@akshar-dave
akshar-dave / AltTitlesForYouTube.js
Last active July 3, 2022 13:50
Set alternate titles for YouTube.
if(window.location.href === "https://www.youtube.com"){
window.location = "https://www.youtube.com/feed/subscriptions"
}
const getCurrentVideoID = () => {
let currentUrl = window.location.href;
let currentVideoID = currentUrl.split("https://www.youtube.com/watch?v=", -1)[1];
return currentVideoID
}
@akshar-dave
akshar-dave / VideosIntersectionObserver.js
Last active April 20, 2022 05:56
Pause videos when not in view 🍿
// pause video playback when not in view
useEffect(() => {
let observerOptions = {
rootMargin: "0px",
threshold: [0.25, 0.75] // start playing when 25% and 75% of the element is in view
};
// watch all selected videos and control playback when they enter the viewport
let handlePlay = (entries, videoObserver) => {
entries.forEach((entry) => {
@akshar-dave
akshar-dave / DelayedInput.js
Last active November 17, 2021 16:41
Executes code after a delay, only if current text input is different from the old text
//Handle input field text after user stops typing (300ms delay), and only execute code if there is new text
// Create a new timeout
let inputTimeout = null;
// Create a variable to store old text
let oldInputText = '';
inputField.addEventListener('keyup', function () {
// Clear the timeout on keypress
@akshar-dave
akshar-dave / MaterialIconsCopyToClipboard.js
Created September 21, 2021 20:12
Material Icons Copy to Clipboard
function $(element){
return document.querySelectorAll(element);
}
function getSvg(url){
fetch(url)
.then(function(response) {
response.text().then(function(text) {
if(document.hasFocus()){
navigator.clipboard.writeText(text);
@akshar-dave
akshar-dave / UnsplashNotificationActivity.js
Last active September 21, 2021 20:15
Open the Activity tab in notifications on Unsplash by default
function $(elem){
return document.querySelectorAll(elem);
}
var notificationBtn = $('button._1aYva')[0];
notificationBtn.addEventListener('click', function(){
var notificationsVisible = setInterval(function() {
if ($('._35qCx button:nth-child(2)')[0]) {
@akshar-dave
akshar-dave / NounProjectDirectDownload.js
Created August 23, 2021 05:44
Directly download from TheNounProject
// fileSaver
(function(a,b){if("function"==typeof define&&define.amd)define([],b);else if("undefined"!=typeof exports)b();else{b(),a.FileSaver={exports:{}}.exports}})(this,function(){"use strict";function b(a,b){return"undefined"==typeof b?b={autoBom:!1}:"object"!=typeof b&&(console.warn("Deprecated: Expected third argument to be a object"),b={autoBom:!b}),b.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(a.type)?new Blob(["\uFEFF",a],{type:a.type}):a}function c(a,b,c){var d=new XMLHttpRequest;d.open("GET",a),d.responseType="blob",d.onload=function(){g(d.response,b,c)},d.onerror=function(){console.error("could not download file")},d.send()}function d(a){var b=new XMLHttpRequest;b.open("HEAD",a,!1);try{b.send()}catch(a){}return 200<=b.status&&299>=b.status}function e(a){try{a.dispatchEvent(new MouseEvent("click"))}catch(c){var b=document.createEvent("MouseEvents");b.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),a.dispatchEvent(b)}}var f="object"=
@akshar-dave
akshar-dave / CopyEyeemTags.js
Created August 23, 2021 05:32
Copy Eyeem Tags
//create Copy Tags button
function createCopyTagsBtn(){
var copyTagsButton = document.createElement("button");
copyTagsButton.innerText = "Copy Tags";
copyTagsButton.classList.add('copyTagsBtn');
copyTagsButton.addEventListener('click', function(){
var res = "";
var elems = document.querySelectorAll("a.g_bubble");
elems.forEach(function(element){