Cursor帮我写的代码

记录由AI工具帮忙写的代码,越来越离不开Cusor了。
作者: Cursor

白名单功能

指令1

Ctrl + K :重写a标签的点击事件,实现跳转链接如果在白名单内则放行跳转,如果不在则提示风险。

输出结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// JavaScript code to rewrite the click event of an "a" tag
// If the link is in the whitelist, allow the navigation
// If not, display a warning message

// Whitelist of allowed URLs
const whitelist = [
"https://www.example1.com",
"https://www.example2.com",
"https://www.example3.com",
];

// Function to handle the click event
function handleClick(event) {
const targetUrl = event.target.href;

// Check if the URL is in the whitelist
if (whitelist.includes(targetUrl)) {
// Allow the navigation
return true;
} else {
// Prevent the navigation and display a warning message
event.preventDefault();
alert("Warning: The link you clicked is not in the whitelist and may be risky.");
return false;
}
}

// Attach the event listener to all "a" tags
const links = document.getElementsByTagName("a");
for (let i = 0; i < links.length; i++) {
links[i].addEventListener("click", handleClick);
}

写的挺好,满足要求,稍微修改下自己就能用了

指令 2

Ctrl + K 实现方法一个白名单网站数组,判断当前的网址是否在白名单内,如果在则放行跳转。

1
2
3
4
5
6
7
8
9
10
let whitelist = ['example.com', 'google.com', 'facebook.com'];

function checkWhitelist(url) {
let hostname = new URL(url).hostname;
return whitelist.includes(hostname);
}

if (checkWhitelist(window.location.href)) {
window.location.href = window.location.href;
}

写的不错满足我的要求,比我自己想的实现方法简单不少。

指令 数组遍历

Ctrl + K js数组遍历写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// ES5 forEach
var arr = [1, 2, 3];
arr.forEach(function(item, index, array) {
console.log(item, index);
});

// ES6 for...of
var arr = [1, 2, 3];
for (let item of arr) {
console.log(item);
}

// ES6 entries() + destructuring
var arr = [1, 2, 3];
for (let [index, item] of arr.entries()) {
console.log(index, item);
}

// ES6 entries() + forEach()
var arr = [1, 2, 3];
arr.entries().forEach(function(item) {
console.log(item[0], item[1]);
});

// ES6 map()
var arr = [1, 2, 3];
arr.map(function(item, index) {
console.log(item, index);
});

// ES6 reduce()
var arr = [1, 2, 3];
arr.reduce(function(prev, curr, index) {
console.log(prev, curr, index);
});

// ES6 filter()
var arr = [1, 2, 3];
arr.filter(function(item, index) {
console.log(item, index);
});

// ES6 find()
var arr = [1, 2, 3];
arr.find(function(item, index) {
console.log(item, index);
});

// ES6 findIndex()
var arr = [1, 2, 3];
arr.findIndex(function(item, index) {
console.log(item, index);
});

// ES6 some()
var arr = [1, 2, 3];
arr.some(function(item, index) {
console.log(item, index);
});

// ES6 every()
var arr = [1, 2, 3];
arr.every(function(item, index) {
console.log(item, index);
});

// for loop
var arr = [1, 2, 3];
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}

// for loop with cached length
var arr = [1, 2, 3];
for (var i = 0, len = arr.length; i < len; i++) {
console.log(arr[i]);
}

// while loop
var arr = [1, 2, 3];
var i = 0;
while (i < arr.length)