main.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { PageMod } = require("sdk/page-mod");
  6. const tabs = require("sdk/tabs");
  7. const { startServerAsync } = require("sdk/test/httpd");
  8. const serverPort = 8099;
  9. exports.testCrossDomainIframe = function(assert, done) {
  10. let server = startServerAsync(serverPort);
  11. server.registerPathHandler("/iframe", function handle(request, response) {
  12. response.write("<html><body>foo</body></html>");
  13. });
  14. let pageMod = PageMod({
  15. include: "about:*",
  16. contentScript: "new " + function ContentScriptScope() {
  17. self.on("message", function (url) {
  18. let iframe = document.createElement("iframe");
  19. iframe.addEventListener("load", function onload() {
  20. iframe.removeEventListener("load", onload, false);
  21. self.postMessage(iframe.contentWindow.document.body.innerHTML);
  22. }, false);
  23. iframe.setAttribute("src", url);
  24. document.documentElement.appendChild(iframe);
  25. });
  26. },
  27. onAttach: function(w) {
  28. w.on("message", function (body) {
  29. assert.equal(body, "foo", "received iframe html content");
  30. pageMod.destroy();
  31. w.tab.close(function() {
  32. server.stop(done);
  33. });
  34. });
  35. w.postMessage("http://localhost:8099/iframe");
  36. }
  37. });
  38. tabs.open({
  39. url: "about:home",
  40. inBackground: true
  41. });
  42. };
  43. exports.testCrossDomainXHR = function(assert, done) {
  44. let server = startServerAsync(serverPort);
  45. server.registerPathHandler("/xhr", function handle(request, response) {
  46. response.write("foo");
  47. });
  48. let pageMod = PageMod({
  49. include: "about:*",
  50. contentScript: "new " + function ContentScriptScope() {
  51. self.on("message", function (url) {
  52. let request = new XMLHttpRequest();
  53. request.overrideMimeType("text/plain");
  54. request.open("GET", url, true);
  55. request.onload = function () {
  56. self.postMessage(request.responseText);
  57. };
  58. request.send(null);
  59. });
  60. },
  61. onAttach: function(w) {
  62. w.on("message", function (body) {
  63. assert.equal(body, "foo", "received XHR content");
  64. pageMod.destroy();
  65. w.tab.close(function() {
  66. server.stop(done);
  67. });
  68. });
  69. w.postMessage("http://localhost:8099/xhr");
  70. }
  71. });
  72. tabs.open({
  73. url: "about:home",
  74. inBackground: true
  75. });
  76. };
  77. require("sdk/test/runner").runTestsFromModule(module);