Check if my javascript is loaded on a site

后端 未结 3 855
我在风中等你
我在风中等你 2020-12-22 00:57

I have asked this question before but it seems i got misunderstood therefore i decided to ask again but this time with alot of information:

For the lazy reader my go

3条回答
  •  醉酒成梦
    2020-12-22 01:58

    Is it possible to check if a site has a certain JavaScript?

    You could do two things, where I think the one using JavaScript would be easier to implement.

    Using PHP:

    • fetch the content from the URLs in your database
    • load it into a DOMDocument and search for a specific part of your code
    • extract all external JavaScript files from that document
      • fetch them too and search for a specific code part within

    Using JavaScript

    • implement an AJAX request within a constructor or an initiate method
    • send the current domain where the script is executed and compare it to the list in your database

    Self hosting

    • as discussed while writing this answer, this is another possibility, as your log can show all pages that this script accessed

    Is it possible to check if the javascript is running without errors?

    This one is a bit more complicated as you have check this during runtime. Imagine the user of your script extends or modifies your code or simply uses it in a wrong way or has something that conflicts with your code. Maybe this code is never reached, as it invokes some user interaction, that may or may not be triggered.

    One possibility would be to send an AJAX request back to you, when an error occured. You can check out window.onerror for that purpose:

    window.onerror = function(message, url, lineNumber) {}
    

    This will give you some detailed information, that you can pass to your server using an AJAX request.

提交回复
热议问题