Files
palemoon27/devtools/client/shared/frame-script-utils.js
roytam1 7f9fb739f6 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1166365 - Pluralize dropdown logic. r=Gijs,Margaret (d99325586b)
- Bug 1234833 - Fix handling of some PostData parameters. r=vporof (bf45f32ba4)
- Bug 1186498 - Let the "cached" of transferredSize can be localized. r=jsantell (0abb05e279)
- bug 1158264 - Flag requests coming from service workers. r=vporof (3650f04b89)
- Bug 1240876 - Don't wait for load to enable "Copy as cURL" command [r=vporof] (7ceaf15b2c)
- Bug 1240959 - Properly calculate total size. r=vporof (67e5bf1cd9)
- Bug 1223037 - Move the network monitor toolbar to the top. r=vporof (ce0606d60e)
- Bug 1244725: DevTools: Show text of a HTTP response for video live streaming content types r=Honza (1a2fb93642)
- Bug 1219771 - allow SVG images to be copied as a data: URI; r=vporof (51cf954fb6)
- Bug 1222583 - Negative url filtering for network monitor. r=vporof (c26e255a12)
- minor revert (16f475356c)
- Bug 1194561 - Change globe to broken lock for insecure resources r=jryans (6766dc0f6d)
- Bug 1252807 - Fix eslint warnings in the Net panel. r=pbro (e77dd377c1)
- Bug 1252346 - Some DevTools files missing Services. r=ochameau (7619d52bef)
- Bug 1252807 - Fix eslint warnings in the Net panel. r=pbro (f913ffcbe5)
- Bug 1235699 - Increase the timeout of browser_animation_toggle_button_resets_on_navigate.js (01e4ad0ce7)
- Bug 1181839 - Include inspector/test/head.js (which imports shared-head.js) in animationinspector's tests and removes code duplication. r=pbro (d855de2b1e)
- Bug 1258305 - Update all occurences of ViewHelpers.L10N and MultiL10N to use the new module, r=jsantell (b080a336c9)
- Bug 1412825 - fix lz4 deprecated attribute with clang and c++14; r=RyanVM (059d86484b)
- Bug 1245886 - Refactor performance tests, r=jsantell (8d01a6a376)
- Bug 1258302 - Create a categories module instead of placing everything in global.js, r=jsantell (5e75d47255)
- Bug 1258309 - Update all occurences of ViewHelpers.Prefs to use the new module, r=jsantell (d28803cb1c)
- fix some 1245886 fallout (366156f165)
2024-05-14 22:16:06 +08:00

209 lines
6.7 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
const {require, loader} = Cu.import("resource://devtools/shared/Loader.jsm", {});
const promise = require("promise");
loader.lazyImporter(this, "Task", "resource://gre/modules/Task.jsm", "Task");
const subScriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Ci.mozIJSSubScriptLoader);
var EventUtils = {};
subScriptLoader.loadSubScript("chrome://marionette/content/EventUtils.js", EventUtils);
loader.lazyGetter(this, "nsIProfilerModule", () => {
return Cc["@mozilla.org/tools/profiler;1"].getService(Ci.nsIProfiler);
});
addMessageListener("devtools:test:history", function ({ data }) {
content.history[data.direction]();
});
addMessageListener("devtools:test:navigate", function ({ data }) {
content.location = data.location;
});
addMessageListener("devtools:test:reload", function ({ data }) {
data = data || {};
content.location.reload(data.forceget);
});
addMessageListener("devtools:test:console", function ({ data: { method, args, id } }) {
content.console[method].apply(content.console, args)
sendAsyncMessage("devtools:test:console:response", { id });
});
/**
* Performs a single XMLHttpRequest and returns a promise that resolves once
* the request has loaded.
*
* @param Object data
* { method: the request method (default: "GET"),
* url: the url to request (default: content.location.href),
* body: the request body to send (default: ""),
* nocache: append an unique token to the query string (default: true)
* }
*
* @return Promise A promise that's resolved with object
* { status: XMLHttpRequest.status,
* response: XMLHttpRequest.response }
*
*/
function promiseXHR(data) {
let xhr = new content.XMLHttpRequest();
let method = data.method || "GET";
let url = data.url || content.location.href;
let body = data.body || "";
if (data.nocache) {
url += "?devtools-cachebust=" + Math.random();
}
let deferred = promise.defer();
xhr.addEventListener("loadend", function loadend(event) {
xhr.removeEventListener("loadend", loadend);
deferred.resolve({ status: xhr.status, response: xhr.response });
});
xhr.open(method, url);
xhr.send(body);
return deferred.promise;
}
/**
* Performs XMLHttpRequest request(s) in the context of the page. The data
* parameter can be either a single object or an array of objects described below.
* The requests will be performed one at a time in the order they appear in the data.
*
* The objects should have following form (any of them can be omitted; defaults
* shown below):
* {
* method: "GET",
* url: content.location.href,
* body: "",
* nocache: true, // Adds a cache busting random token to the URL
* }
*
* The handler will respond with devtools:test:xhr message after all requests
* have finished. Following data will be available for each requests
* (in the same order as requests):
* {
* status: XMLHttpRequest.status
* response: XMLHttpRequest.response
* }
*/
addMessageListener("devtools:test:xhr", Task.async(function* ({ data }) {
let requests = Array.isArray(data) ? data : [data];
let responses = [];
for (let request of requests) {
let response = yield promiseXHR(request);
responses.push(response);
}
sendAsyncMessage("devtools:test:xhr", responses);
}));
addMessageListener("devtools:test:profiler", function ({ data: { method, args, id }}) {
let result = nsIProfilerModule[method](...args);
sendAsyncMessage("devtools:test:profiler:response", {
data: result,
id: id
});
});
// To eval in content, look at `evalInDebuggee` in the shared-head.js.
addMessageListener("devtools:test:eval", function ({ data }) {
sendAsyncMessage("devtools:test:eval:response", {
value: content.eval(data.script),
id: data.id
});
});
addEventListener("load", function() {
sendAsyncMessage("devtools:test:load");
}, true);
/**
* Set a given style property value on a node.
* @param {Object} data
* - {String} selector The CSS selector to get the node (can be a "super"
* selector).
* - {String} propertyName The name of the property to set.
* - {String} propertyValue The value for the property.
*/
addMessageListener("devtools:test:setStyle", function(msg) {
let {selector, propertyName, propertyValue} = msg.data;
let node = superQuerySelector(selector);
if (!node) {
return;
}
node.style[propertyName] = propertyValue;
sendAsyncMessage("devtools:test:setStyle");
});
/**
* Set a given attribute value on a node.
* @param {Object} data
* - {String} selector The CSS selector to get the node (can be a "super"
* selector).
* - {String} attributeName The name of the attribute to set.
* - {String} attributeValue The value for the attribute.
*/
addMessageListener("devtools:test:setAttribute", function(msg) {
let {selector, attributeName, attributeValue} = msg.data;
let node = superQuerySelector(selector);
if (!node) {
return;
}
node.setAttribute(attributeName, attributeValue);
sendAsyncMessage("devtools:test:setAttribute");
});
/**
* Synthesize a key event for an element. This handler doesn't send a message
* back. Consumers should listen to specific events on the inspector/highlighter
* to know when the event got synthesized.
* @param {Object} msg The msg.data part expects the following properties:
* - {String} key
* - {Object} options
*/
addMessageListener("Test:SynthesizeKey", function(msg) {
let {key, options} = msg.data;
EventUtils.synthesizeKey(key, options, content);
});
/**
* Like document.querySelector but can go into iframes too.
* ".container iframe || .sub-container div" will first try to find the node
* matched by ".container iframe" in the root document, then try to get the
* content document inside it, and then try to match ".sub-container div" inside
* this document.
* Any selector coming before the || separator *MUST* match a frame node.
* @param {String} superSelector.
* @return {DOMNode} The node, or null if not found.
*/
function superQuerySelector(superSelector, root=content.document) {
let frameIndex = superSelector.indexOf("||");
if (frameIndex === -1) {
return root.querySelector(superSelector);
} else {
let rootSelector = superSelector.substring(0, frameIndex).trim();
let childSelector = superSelector.substring(frameIndex+2).trim();
root = root.querySelector(rootSelector);
if (!root || !root.contentWindow) {
return null;
}
return superQuerySelector(childSelector, root.contentWindow.document);
}
}