嗨,我正在尝试为 Google 扩展程序自动化一段代码以更改地理位置。我希望它与激活/停用按钮一起使用:当我从“Content.js”中删除 IF 语句时,它工作正常,但使用 IF 语句它不起作用。请帮忙。
选项.html
<button id="activate-it">Activate!</button>
<button id="deactivate-it">Deactivate!</button>
背景.Js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
{
if (request.method == "getStatus")
{
sendResponse({status: 'activate-it'});
}
});
内容.Js
chrome.runtime.sendMessage({method: "getStatus"}, function(response)
{
if (response.status == "activate-it")
{
var lat = "28.22";
var lon = "70.86";
var script = document.createElement('script');
script.innerHTML = 'navigator.geolocation.getCurrentPosition=function(a,b){a({coords:{latitude:'+lat+',longitude:'+ lon +'},timestamp:Date.now()})};var position={coords:{latitude:'+lat+',longitude:'+lon+'}};';
document.head.insertBefore(script, document.head.firstChild);
}
else if (response.status!="activate-it")
{
var scripts = document.getElementsByTagName('script');
for(var i = 0; i < scripts.length; i++)
{
if(scripts[i].text.indexOf("coords") !== -1)
{
scripts[i].text = scripts[i].text.replace(/position\.coords\.latitude/g, lat);
scripts[i].text = scripts[i].text.replace(/position\.coords\.longitude/g, lon);
scripts[i].text = scripts[i].text.replace(/coords\.latitude/g, lat);
scripts[i].text = scripts[i].text.replace(/coords\.longitude/g, lon);
}
}
}
});