mirror of
https://gitee.com/bitdance-team/chrome-extension
synced 2025-10-07 16:35:15 +08:00
Merge branch 'trans_zl_2-9' into develop
This commit is contained in:
@@ -48,3 +48,13 @@ chrome.contextMenus.create({
|
||||
showNotification()
|
||||
}
|
||||
})
|
||||
|
||||
function tranBit(){
|
||||
console.log("开始插入翻译页面")
|
||||
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
||||
console.log(tabs)
|
||||
chrome.tabs.executeScript(tabs[0].id, { file:'./assets/js/translate/tran.js', runAt: 'document_start' })
|
||||
chrome.tabs.insertCSS(tabs[0].id, { file:'./assets/css/tran.css', runAt: 'document_start' })
|
||||
})
|
||||
}
|
97
packages/shell-chrome/assets/js/popup2.js
Normal file
97
packages/shell-chrome/assets/js/popup2.js
Normal file
@@ -0,0 +1,97 @@
|
||||
//天气
|
||||
let key_w = "314285a761da4025bd4c09339dca5f0f"
|
||||
var longitude = 116.40
|
||||
var latitude = 39.90
|
||||
|
||||
var searchWeatherfn = async function () {
|
||||
// 用经纬度获取
|
||||
let url_jw = longitude + ',' + latitude
|
||||
console.log("url_jw: " + url_jw)
|
||||
let httpUrl = `https://geoapi.qweather.com/v2/city/lookup?location=${url_jw}&key=${key_w}`
|
||||
//获取城市的ID
|
||||
// let httpUrl = `https://geoapi.qweather.com/v2/city/lookup?location=黔江区&adm=重庆&key=${key}`
|
||||
let res1 = await fetch(httpUrl) // 不能res,会冲突
|
||||
let result = await res1.json()
|
||||
console.log("下面是result")
|
||||
console.log(result)
|
||||
document.getElementById("weatherSite").innerHTML = result.location[0].adm1
|
||||
let id = result.location[0].id
|
||||
console.log(id)
|
||||
//根据城市id获取具体的天气
|
||||
let httpUrl1 = `https://devapi.qweather.com/v7/weather/now?location=${id}&key=${key_w}`
|
||||
let res2 = await fetch(httpUrl1)
|
||||
let result1 = await res2.json()
|
||||
|
||||
console.log(result1)
|
||||
console.log(result1.now)
|
||||
//显示天气情况
|
||||
let now = result1.now.text
|
||||
document.getElementById("weatherText").innerHTML = now
|
||||
//显示温度
|
||||
let nowTemp = result1.now.temp
|
||||
document.getElementById("weatherTemp").innerHTML = nowTemp + '°'
|
||||
//显示图标
|
||||
let svg_icon = result1.now.icon
|
||||
document.getElementById("svg_w").src = "./assets/image/weather/icons/" + svg_icon + ".svg"
|
||||
//切换背景色
|
||||
var bobyEI = document.body
|
||||
svg_icon = parseInt(svg_icon)
|
||||
if (100 <= svg_icon && svg_icon < 101) {
|
||||
bobyEI.style.backgroundImage = "url(./assets/image/weather/sun.webp)"
|
||||
} else if (101 <= svg_icon && svg_icon < 104) {
|
||||
bobyEI.style.backgroundImage = "url(./assets/image/weather/yun.webp)"
|
||||
} else if (104 <= svg_icon && svg_icon < 150) {
|
||||
bobyEI.style.backgroundImage = "url(./assets/image/weather/yin.webp)"
|
||||
} else if (150 <= svg_icon && svg_icon < 300) {
|
||||
bobyEI.style.backgroundImage = "url(./assets/image/weather/night.webp)"
|
||||
} else if (300 <= svg_icon && svg_icon < 400) {
|
||||
bobyEI.style.backgroundImage = "url(./assets/image/weather/yu.webp)"
|
||||
} else if (400 <= svg_icon && svg_icon < 500) {
|
||||
bobyEI.style.backgroundImage = "url(./assets/image/weather/xue.webp)"
|
||||
} else {
|
||||
bobyEI.style.backgroundImage = "url(./assets/image/weather/yun.webp)"
|
||||
}
|
||||
}
|
||||
|
||||
function find_site() { //获取浏览器的经纬度
|
||||
console.log("开始定位浏览器位置")
|
||||
localStorage.removeItem('BitD_longitude')
|
||||
localStorage.removeItem('BitD_latitude')
|
||||
navigator.geolocation.getCurrentPosition(onSuccess); // 获取经纬度
|
||||
function onSuccess(position) {
|
||||
//经度
|
||||
longitude = position.coords.longitude;
|
||||
localStorage.setItem('BitD_longitude', longitude)
|
||||
console.log("longitude:" + longitude)
|
||||
|
||||
//纬度
|
||||
latitude = position.coords.latitude;
|
||||
localStorage.setItem('BitD_latitude', latitude)
|
||||
console.log("latitude:" + latitude)
|
||||
console.log("获得浏览器经纬度而且写入localstorage end")
|
||||
}
|
||||
}
|
||||
|
||||
if (localStorage.getItem('BitD_longitude')) { //没有缓存
|
||||
longitude = localStorage.getItem('BitD_longitude')
|
||||
latitude = localStorage.getItem('BitD_latitude')
|
||||
searchWeatherfn()
|
||||
} else { // 没有缓存的时候 或者 需要更新的时候
|
||||
console.log("没有找到")
|
||||
find_site()
|
||||
searchWeatherfn()
|
||||
}
|
||||
|
||||
document.getElementById("weatherSite").onclick = function () {
|
||||
find_site()
|
||||
searchWeatherfn()
|
||||
}
|
||||
|
||||
// //天气end
|
||||
//翻译
|
||||
document.getElementById("ToTans").onclick = function () {
|
||||
console.log(chrome.extension.getBackgroundPage())
|
||||
chrome.extension.getBackgroundPage().tranBit()
|
||||
}
|
||||
|
||||
//翻译end
|
87
packages/shell-chrome/assets/js/translate/tran.js
Normal file
87
packages/shell-chrome/assets/js/translate/tran.js
Normal file
@@ -0,0 +1,87 @@
|
||||
if (typeof (BitTarnsDiv) !== "undefined") {
|
||||
console.log("存在 BitTarnsDiv")
|
||||
BitTarnsDiv.parentNode.removeChild(BitTarnsDiv)
|
||||
}
|
||||
var BitTarnsDiv = document.createElement("div")
|
||||
BitTarnsDiv.id = "BitTarnsDivId"
|
||||
document.body.appendChild(BitTarnsDiv)
|
||||
BitTarnsDiv.style.backgroundColor = "red"
|
||||
BitTarnsDiv.style.height = "450px"
|
||||
BitTarnsDiv.style.width = "300px"
|
||||
BitTarnsDiv.style.zIndex = 99999
|
||||
BitTarnsDiv.style.position = "fixed"
|
||||
BitTarnsDiv.style.right = '10px'
|
||||
BitTarnsDiv.style.top = '50px'
|
||||
BitTarnsDiv.style.float = 'right'
|
||||
|
||||
|
||||
BitTarnsDiv.innerHTML = `
|
||||
<div id = 'trans'>
|
||||
<select id="inputLangSelect">
|
||||
<option value="auto">自动检测语言</option>
|
||||
<option value="zh">简体中文</option>
|
||||
<option value="en">English</option>
|
||||
</select>
|
||||
<select id="outLangSelect">
|
||||
<option value="zh">简体中文</option>
|
||||
<option value="en">English</option>
|
||||
</select>
|
||||
<button id="deleteTrans">关闭</button>
|
||||
<input type="text" id="inputLang" value="" placeholder="请输入翻译内容" >
|
||||
<br>
|
||||
自动检测语言:<p id="LangType"></p><button id="tranbtn">翻译</button>
|
||||
<hr>
|
||||
<p id="outPutRes"></p>
|
||||
</div>
|
||||
`
|
||||
|
||||
var from = 'auto';
|
||||
var to = 'auto';
|
||||
|
||||
var postMsg = async function () {
|
||||
from = document.getElementById("inputLangSelect").value
|
||||
to = document.getElementById("outLangSelect").value
|
||||
var msgq = document.getElementById("inputLang").value
|
||||
// 翻译api
|
||||
msgq = encodeURIComponent(msgq)
|
||||
|
||||
let httpUrl1 = "//qca566.api.cloudendpoint.cn/hello?msgq=" +
|
||||
msgq + "&from=" + from + "&to=" + to + ""
|
||||
let res1 = await fetch(httpUrl1)
|
||||
let result1 = await res1.json()
|
||||
let res2 = result1.trans_result[0].dst
|
||||
|
||||
document.getElementById("outPutRes").innerHTML = res2
|
||||
}
|
||||
|
||||
var onInputChange = async function () {
|
||||
|
||||
var msgq = document.getElementById("inputLang").value
|
||||
msgq = encodeURIComponent(msgq)
|
||||
let httpUrl1 = "//qca566.api.cloudendpoint.cn/transLang?msgq=" + msgq + ""
|
||||
let res1 = await fetch(httpUrl1)
|
||||
let result1 = await res1.json()
|
||||
let res2 = result1.data.src
|
||||
if (res2 == 'en') {
|
||||
document.getElementById("LangType").innerHTML = '英文'
|
||||
} else if (res2 == 'zh') {
|
||||
document.getElementById("LangType").innerHTML = '简体中文'
|
||||
} else {
|
||||
document.getElementById("LangType").innerHTML = res
|
||||
}
|
||||
postMsg()
|
||||
}
|
||||
|
||||
//触发识别语言,再翻译
|
||||
document.getElementById("inputLang").onchange = function () {
|
||||
onInputChange()
|
||||
}
|
||||
//直接翻译
|
||||
document.getElementById("tranbtn").onclick = function () {
|
||||
postMsg()
|
||||
}
|
||||
//关闭翻译模块
|
||||
document.getElementById("deleteTrans").onclick = function () {
|
||||
BitTarnsDiv.parentNode.removeChild(BitTarnsDiv)
|
||||
BitTarnsDiv = undefined
|
||||
}
|
Reference in New Issue
Block a user