HEllo all:我必须是一个完全的菜鸟 - 我在我的html文件的头部有这两个javascript脚本,但我想隐藏api号码,还有第二个js。我需要写什么来将这些js放在另一个文件中,当右键单击鼠标/显示源代码时,没有人可以读取这些js,并且在div中具有相同的谷歌地图
我试图把src =“filename.js”但谷歌地图根本不会加载。
模糊JS代码?但是我们怎么能把它放在旁边文件中呢? src =“obfuscated code.js”根本不会加载谷歌地图 只是把未经删除的代码放在头脑中,但它会搞砸谷歌索引吗?
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=
...&sensor=false"></script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(-19.991901,57.592607);
var settings = {
zoom: 1,
disableDoubleClickZoom: true,
draggable: false,
scrollwheel: false,
minZoom: 1,
maxZoom: 18,
center: latlng,
mapTypeControl: false,
navigationControl: true,
navigationControlOptions: {style: google.maps.NavigationControlStyle.BIG},
mapTypeId: google.maps.MapTypeId.ROADMAP};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
var companyImage = new google.maps.MarkerImage('images/logo.png',
new google.maps.Size(100,50),
new google.maps.Point(0,0),
new google.maps.Point(50,50)
);
var companyShadow = new google.maps.MarkerImage('images/logo_shadow.png',
new google.maps.Size(130,50),
new google.maps.Point(0,0),
new google.maps.Point(65, 50));
var companyPos = new google.maps.LatLng(-19.9913,57.592607);
var companyMarker = new google.maps.Marker({
position: companyPos,
map: map,
icon: companyImage,
shadow: companyShadow,
title:"Bur-Nas Beach",
zIndex: 3});
google.maps.event.addListener(companyMarker, 'click', function() {
infowindow.open(map,companyMarker);
});
}
</script>
答案 0 :(得分:2)
好的有一种方法可以通过动态加载你的javascript(这意味着动态地通过其他一些javascript注入脚本标记)来实现这一点,然后在它保留在浏览器内存中时将其删除。
(function() {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"https://maps.googleapis.com/maps/api/js?key=YOURKEYHERE");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
main();
}
};
} else { // Other browsers
script_tag.onload = main;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
function removejscssfile(filename, filetype){
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
var allsuspects=document.getElementsByTagName(targetelement)
for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
}
}
/******** Our main function called when Maps has loaded ********/
function main() {
//initiate maps here
//remove all occurences of your script on page
removejscssfile("https://maps.googleapis.com/maps/api/js?key=YOURKEYHERE", "js") //remove all occurences of your script on page
//Do some cool googlemaps based stuff here
}
})(); // We call our anonymous function immediately
答案 1 :(得分:0)
其中一个解决方案是对文件进行模糊处理,使其不具备人类可读性。 See this thread