什么是关键词 #
当你在搜索引擎中输出字符的时候,会有一个下拉框显示一些和关键词列表,用来提醒你想搜啥。
间接上代码
/**
* 获取各大搜索引擎的关键字列表
*/
/** 获取随机小写字母 */
function getRandomLetters(number) {
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
let value = '';
for (let i = 0; i < number; i++) {value = value + alphabet[Math.floor(Math.random() * alphabet.length)];
}
return value;
}
/** 获取百度关键词列表 */
let dataMap = {}; // 缓存列表
function getBaiDuSug(searchValue){return new Promise((r)=>{if(dataMap[searchValue]){r(dataMap[searchValue]);
return;
}
/** 创立一个惟一标识用于承受数据,避免数据对应不齐 */
let sign = `sug_sign_${getRandomLetters(3)}_${new Date().getTime()}`;
window[sign] = (data)=>{setTimeout(()=>{sug.remove();},1000); // 删除元素
let list = data.s || [];
dataMap[searchValue] = list;
r(list);
}
let sug = document.createElement('script');
sug.src = `https://suggestion.baidu.com/su?wd=${encodeURIComponent(searchValue || '')}&cb=window.${sign}`;
document.getElementsByTagName('body')[0].appendChild(sug);
});
}
/** 获取 bing 关键词列表 */
let dataMap_1 = {}; // 缓存列表
function getBingSug(searchValue){return new Promise((r)=>{if(dataMap_1[searchValue]){r(dataMap_1[searchValue]);
return;
}
/** 创立一个惟一标识用于承受数据,避免数据对应不齐 */
let sign = `sug_sign_${getRandomLetters(3)}_${new Date().getTime()}`;
window[sign] = (data)=>{setTimeout(()=>{sug.remove();},1000); // 删除元素
let list = [];
let list_1 = data.AS.Results || [];
list_1.forEach(item=>{
list.push(...item.Suggests.map(item_=>{return item_.Txt;}));
});
dataMap_1[searchValue] = list;
r(list);
}
let sug = document.createElement('script');
sug.src = `https://api.bing.com/qsonhs.aspx?type=cb&q=${encodeURIComponent(searchValue || '')}&cb=window.${sign}`;
document.getElementsByTagName('body')[0].appendChild(sug);
});
}
/** 获取 360 关键词列表 */
let dataMap_2 = {}; // 缓存列表
function get360Sug(searchValue){return new Promise((r)=>{if(dataMap_2[searchValue]){r(dataMap_2[searchValue]);
return;
}
/** 创立一个惟一标识用于承受数据,避免数据对应不齐 */
let sign = `sug_sign_${getRandomLetters(3)}_${new Date().getTime()}`;
window[sign] = (data)=>{setTimeout(()=>{sug.remove();},1000); // 删除元素
let list = [];
let list_1 = data.result || [];
list_1.forEach(item=>{list.push(item.word || '');
});
dataMap_2[searchValue] = list;
r(list);
}
let sug = document.createElement('script');
sug.src = `https://sug.so.360.cn/suggest?encodein=utf-8&encodeout=utf-8&format=json&word=${encodeURIComponent(searchValue || '')}&callback=window.${sign}`;
document.getElementsByTagName('body')[0].appendChild(sug);
});
}
/** 获取淘宝关键词列表 */
let dataMap_3 = {}; // 缓存列表
function getTaoBaoSug(searchValue){return new Promise((r)=>{if(dataMap_3[searchValue]){r(dataMap_3[searchValue]);
return;
}
/** 创立一个惟一标识用于承受数据,避免数据对应不齐 */
let sign = `sug_sign_${getRandomLetters(3)}_${new Date().getTime()}`;
window[sign] = (data)=>{setTimeout(()=>{sug.remove();},1000); // 删除元素
let list = [];
let list_1 = data.result || [];
list_1.forEach(item=>{list.push(item[0] || '');
});
dataMap_3[searchValue] = list;
r(list);
}
let sug = document.createElement('script');
sug.src = `https://suggest.taobao.com/sug?code=utf-8&q=${encodeURIComponent(searchValue || '')}&callback=window.${sign}`;
document.getElementsByTagName('body')[0].appendChild(sug);
});
}
不便当前间接复制应用,应用例子
getBingSug('苹果').then(e=>{console.log(e);
})
十分的好用哇,为什么要有个 sign 标识呢,因为屡次调用会存在一个问题,就是不晓得这次的列表是对应我输出的什么字符的,当初这个完满解决了这个问题。
原文