简体   繁体   中英

Javascript domain protect script

I have made a .js file and I would like to protect it a little. Just from small leechers, obviously more advanced people will be able to figure it out.

I'd like to add to my script, something to check which domain the file is being run on. If it's not my two domains ( example1.com , example2.com ), it will redirect to my domain. Sort of like an iframe buster.

Then I can take the whole code and obfuscate it a little. My purpose is to prevent someone from downloading the .js and using it on their server.

My code:

if(typeof(width)=='undefined')width=655;
if(typeof(height)=='undefined')height=540;
if(width<655)width=655;if(height<540)height=540;
if(width<height)height=width;
document.write('<ifr'+'ame src="/streams/hd/'+ ch+'.php" scrolling=no frameborder=0 width='+ width+' height='+ height+' scrolling=no allowtransparency=true id=thatframe ></ifr'+'ame>');

Is there something I can add to this that will only allow the script to be run on my websites? example1.com , example2.com ?

Use document.location.host that returns the host name, then check in your script like

var _host = document.location.host;
if( _host == "example1.com" || _host == "example2.com" ){
    // do things for example1.com
}

If you want to protect your JS file then use jscompress the compressed version.

Pretty basic solution, I know there are better ways to get the current URL.

if (document.URL.indexOf('http://example.com') == 0) {
   // Then you're on example.com.
}

Note that this works for subfolders also, eg http://example.com/foo/bar .

You can also use document.domain like this ( MDN Reference for document.domain)

var allowedDomains = ['example1.com', 'example2.com'];

if (allowedDomains.indexOf(document.domain) == -1) {
  // Code to redirect here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM