最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Injecting javascript into zombie.js - Stack Overflow

programmeradmin0浏览0评论

Hi I was wondering if there is the ability in node js and zombie js to inject javascript files in to the headless browser, similar to what you can do with phantomjs.

For example in phantom js you would do:

page.injectJs("amino/TVI.js")

I have used phantomjs and it does do what I want it to do but however I am testing other options due to the high memory required by using phantom js.

Hi I was wondering if there is the ability in node js and zombie js to inject javascript files in to the headless browser, similar to what you can do with phantomjs.

For example in phantom js you would do:

page.injectJs("amino/TVI.js")

I have used phantomjs and it does do what I want it to do but however I am testing other options due to the high memory required by using phantom js.

Share Improve this question edited Apr 10, 2014 at 13:21 devnull69 16.6k9 gold badges54 silver badges65 bronze badges asked Apr 10, 2014 at 13:19 CharabonCharabon 7873 gold badges11 silver badges24 bronze badges 1
  • Are you familiar with how bookmarklets work? Maybe that could help you do what you're trying to acplish. – user3507600 Commented Apr 10, 2014 at 13:23
Add a ment  | 

2 Answers 2

Reset to default 6

you can append script tag into document object since it support DOM API in zombie.

The following example shows how to insert jquery into zombie homepage:

var Browser = require("zombie");
var assert = require("assert");    

// Load the page from localhost
browser = new Browser()
browser.visit("http://zombie.labnotes/", function () {    

  assert.ok(browser.success);

  // append script tag
  var injectedScript = browser.document.createElement("script");
  injectedScript.setAttribute("type","text/javascript");
  injectedScript.setAttribute("src", "http://code.jquery./jquery-1.11.0.min.js");    

  browser.body.appendChild(injectedScript);    

  browser.wait(function(window) {
    // make sure the new script tag is inserted
    return window.document.querySelectorAll("script").length == 4;
  }, function() {
    // jquery is ready
    assert.equal(browser.evaluate("$.fn.jquery"), "1.11.0");
    console.log(browser.evaluate("$('title').text()"));
  });
});

Try to think the other way around. You have already everything at your hand in zombie to inject everything you want.

For example: that.browser.window points to the jsdom window that every part of your site javascript is using as a base. So you can access the dom and all other window objects in the page already loaded.

I don't know what you want to archieve with injecting - you should not use it for testing anway, but it looks this is not your actual goal

发布评论

评论列表(0)

  1. 暂无评论