检查我的javascript是否已加载到网站上 [英] Check if my javascript is loaded on a site

查看:80
本文介绍了检查我的javascript是否已加载到网站上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前已经问过这个问题,但似乎我被误解了,所以我决定再次询问,但这一次有很多信息:

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 goal here is:


有没有办法检查是通过PHP还是通过Ajax在外部站点上启用了javascript

Is there a way to check if a javascript is enabled on an external site either from PHP or through Ajax

详细说明:

好的,我的公司,有很多客户,当这些客户在我们的网站上注册时,他们会收到 javascript 。然后他们可以在自己的网站上使用这个脚本来获得一些额外的功能。

Okay, so my company, has many clients, when these clients sign up on our website, they are handed a javascript. They can then use this script on their own website to get some extra features.

现在所有的网站,客户拥有的,存储在我们的CMS系统中(就像他们所有与我们签约的网站一样)。

Now all of the websites, that a client has, is stored within our CMS system (like there is a list of all of the websites they have that are signed with us).

现在我的老板给了我有以下任务:

Now my boss gave me the following assignment:


我们需要一个功能,以便用户可以按下测试按钮,系统将测试是否有javascript是在他们的网站上工作没有错误(即是否有另一个脚本阻止我们)。

We need a function so that the user can press the button "Test" and the system will test if the javascript is on their site and working without errors (i.e is there another script blocking ours).

现在过去几天,我有一直试图在 web 上找到类似的案例,我唯一建议的是Phantomjs但是没有关于如何接近我想达到的目标的例子。

Now the past couple of days, i have been trying to find similar cases on the web and the only thing i have been suggested, was Phantomjs however there are NO examples on how to even get close to what i want to achieve.

所以我的问题是:


  1. 是否有可能检查是否一个网站有一定的javascript。

  2. 是否有可能ch如果javascript正在运行而没有错误(如果控制台抛出错误),那么这就好了。

我希望,这次我不会被误解:)如果您有任何疑问,请留言,我会尽快回复!

I hope, that i wont get misunderstood this time :) if you have any questions, please just leave a message, and i will respond as fast as possible!

幻影解决方案(由Elias发布)

这是我的代码:

   var page = require('webpage').create();
page.onConsoleMessage = function( message, line, srcId)
{//show console errors for page
    console.log('Page has errors showing in console: ' + msg
        + ' (line: ' + (line || '?') + ', id: ' + srcId + ')');
};
page.open('http://www.marcrasmussen.com',function(status)
{
    if (status != 'success')
    {//can't load page
        console.log('Failed to open page: ' + status);
        phantom.exit();
        return;
    }
    var reslt = page.evaluate(function()
    {
        var usesOurScript = false,
            scripts = document.querySelectorAll('script');//get all script tags
        Array.forEach.apply(scripts, [function(elem)
        {//check all loaded scripts
              if (/jquery\.js/.js.js.test(elem.getAttribute('src')))
            {//this src attribute refers your script
                usesOurScript = true;//set to true
            }
        }]);
        return {page: location.href, found: usesOurScript};//return its href and the use-status
    });
    //script was found? adjust message
    reslt.found = (reslt.found ? ' uses ' : ' does not use ') + 'our script!';
    console.log(reslt.page + reslt.found);//logs url does not use || uses our script
    phantom.exit();
});

当我从php运行它时,没有输出它只是一直运行:

And there is no output it just keeps running forever when i run it from php:

Test.php

<?php echo shell_exec('phantomjs hello.js');

?>


推荐答案

我也会在这里发布。因为我是那个让你进入phantomjs的家伙,所以我认为你能解决问题的方法是公平的。

以下脚本在无头幻像浏览器中打开一个页面,记录页面在实际浏览器中产生的任何控制台消息(使用 onConsoleMessage 事件),如果页面无法加载,它还会记录错误,并且,如果页面没有问题,它会检查该页面上的所有< script> 标记,查找您的javascript文件。

I'll post it here, too. Since I was the guy that put you on to phantomjs, it's only fair to provide you with, what I think, is the answer to your problems.
The following script opens a page in the headless phantom browser, logs any console messages that page would yield in an actual browser, (using the onConsoleMessage event), it also logs the error if the page were to be unable to load, and, if the page was opened without issues, it checks all <script> tags on that page, looking for your javascript file.

var page = require('webpage').create();
page.onConsoleMessage = function( message, line, srcId)
{//show console errors for page
    console.log('Page has errors showing in console: ' + msg
                + ' (line: ' + (line || '?') + ', id: ' + srcId + ')');
};
page.open('http://www.your-url-here.org',function(status)
{
    if (status != 'success')
    {//can't load page
        console.log('Failed to open page: ' + status);
        phantom.exit();
        return;
    }
    var reslt = page.evaluate(function()
    {
        var usesOurScript = false,
        scripts = document.querySelectorAll('script');//get all script tags
        /* Array.forEach is causing errors, as it turns out...
        Array.forEach.apply(scripts, [function(elem)
        {//check all loaded scripts
            if (/yourScriptName\.js/.test(elem.getAttribute('src')))
            {//this src attribute refers your script
                usesOurScript = true;//set to true
            }
        }]);*/
        for (var i=0;i<scripts.length;i++)
        {
            if (/yourScriptName\.js/.test(scripts[i].getAttribute('src')))
            {
                return {page: location.href, found: true};
            }
        }
        return {page: location.href, found: false};
    });
    //script was found? adjust message
    reslt.found = (reslt.found ? ' uses ' : ' does not use ') + 'our script!';
    console.log(reslt.page + reslt.found);//logs url does not use || uses our script
    phantom.exit();
});

如果该文件的作用类似于jQ(创建对某个对象的全局引用),您甚至可以添加这到 page.evaluate 回调:

If that file is does something like jQ (create a global reference to some object) you might even add this to the page.evaluate callback:

    var reslt = page.evaluate(function()
    {//this code will behave like it's running on the target page
        var libs = {jQuery: ($ || jQuery),
                    myLib: ourLibsGlobalName};
        return libs;
    });
    if (reslt.jQuery instanceof Object)
    {
        console.log('client uses jQ');
    }
    if (reslt.myLib instanceof Object)
    {
        console.log('client uses our lib, successfuly');//wouldn't be accessible if it contained errors
    }
    console.log(reslt.myLib);//shows full object, so you can verify it's the right one

注意:

此代码未经测试,我刚刚将其写下来我的头它可能包含愚蠢的拼写错误。这不是你所有祈祷的副本可回答的答案。

我相信 page.evaluate 回调能够返回一些东西,但是:它在加载页面的上下文中运行,并且是沙箱,因此您可能必须强制错误:

Note:
This code is un-tested, I just wrote it off the top of my head. It may contain silly typo's. It's not a copy-pastable answer to all of your prayers.
I do believe the page.evaluate callback is able to return something, however: it's running in the context of the loaded page, and is sandboxed, so you may have to force an error:

page.evaluate(function()
{
    Array.forEach(document.querySelectorAll('script'), [function(elem)
    {
        if (/yourScript\.js/.test(elem.getAttribute('src')))
        {
            throw {message: 'Script found',
                   nodeSrc: elem.getAttribute('src'),
                   pageUrl: location.href,
                   myLib  : yourGlobalVar}//<-- cf remarks on jQ-like usage
        }
    }]);
});

通过抛出这个未被捕获的自定义错误,所以它最终会在控制台,您强制 page.onConsoleMessage 处理程序来处理此异常。在某种程度上,如果它们导致无限循环问题发生,这可能是围绕沙箱限制的一种方式。

By throwing this custom error, which isn't being caught, so it'll end up in the console, you force the page.onConsoleMessage handler to deal with this exception. That could be a way around the sandbox restrictions, in a way, if they're causing the infinite loop issue to occur.

这篇关于检查我的javascript是否已加载到网站上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆