gpt4 book ai didi

javascript - 在 head 中隐藏 javascript

转载 作者:行者123 更新时间:2023-12-02 18:50:22 24 4
gpt4 key购买 nike

大家好:我一定是个菜鸟 -我的 html 文件头部有这两个 javascript 脚本,但我想隐藏 api 编号以及第二个 js。我需要写什么来将这些js放在另一个文件中,右键单击鼠标/显示源代码时没有人可以读取,并且在div中出现相同的谷歌地图

我尝试输入 src="filename.js"但谷歌地图根本无法加载。

混淆 JS 代码?但是我们怎样才能把它放在一个辅助文件中呢? src="obfuscated code.js"根本不会加载谷歌地图只将混淆的代码放在头部,但它会扰乱谷歌索引吗?

  1.  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=   
    ...&sensor=false"></script>
  2.     <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>

最佳答案

好的,有一种方法可以通过动态加载 JavaScript(这意味着通过其他 JavaScript 动态注入(inject)脚本标记)并在其保留在浏览器内存中时将其删除来实现此目的。

    (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

关于javascript - 在 head 中隐藏 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15908044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com