记录浏览器的一些操作

90 阅读1分钟

1、复制内容到剪切板

const copyToClipboard = (text) => navigator.clipboard.writeText(text);
copyToClipboard("Hello World");

2、清除所有cookie

const clearCookies = document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`));

3、获取选中的文本

const getSelectedText = () => window.getSelection().toString();
getSelectedText();

4、检测是否是黑暗模式

const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
console.log(isDarkMode)

5、 滚动到页面顶部

const goToTop = () => window.scrollTo(0, 0);
goToTop();

6、 判断当前标签页是否激活

const isTabInView = () => !document.hidden; 

7、判断当前是否是苹果设备

const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform);
isAppleDevice();

8、是否滚动到页面底部

const scrolledToBottom = () => document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight;

9、重定向到一个URL

const redirect = url => location.href = url
redirect("https://www.google.com/")

10、打开浏览器打印框

const showPrintDialog = () => window.print()