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

查看:29
本文介绍了检查我的 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. 是否可以检查 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!

phantomJs 解决方案(最初由 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天全站免登陆