messages.js (3973B)
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 5 "use strict"; 6 7 const { 8 prepareMessage, 9 getNaturalOrder, 10 } = require("resource://devtools/client/webconsole/utils/messages.js"); 11 const { 12 IdGenerator, 13 } = require("resource://devtools/client/webconsole/utils/id-generator.js"); 14 const { 15 batchActions, 16 } = require("resource://devtools/client/shared/redux/middleware/debounce.js"); 17 18 const { 19 CSS_MESSAGE_ADD_MATCHING_ELEMENTS, 20 MESSAGE_CLOSE, 21 MESSAGE_OPEN, 22 MESSAGE_REMOVE, 23 MESSAGE_TYPE, 24 MESSAGES_ADD, 25 MESSAGES_CLEAR, 26 MESSAGES_DISABLE, 27 NETWORK_MESSAGES_UPDATE, 28 NETWORK_UPDATES_REQUEST, 29 PRIVATE_MESSAGES_CLEAR, 30 TARGET_MESSAGES_REMOVE, 31 } = require("resource://devtools/client/webconsole/constants.js"); 32 33 const defaultIdGenerator = new IdGenerator(); 34 35 function messagesAdd(resources, idGenerator = null, persistLogs = false) { 36 if (idGenerator == null) { 37 idGenerator = defaultIdGenerator; 38 } 39 const messages = []; 40 for (const resource of resources) { 41 const message = prepareMessage(resource, idGenerator, persistLogs); 42 if (message) { 43 messages.push(message); 44 } 45 } 46 // Sort the messages by their timestamps. 47 messages.sort(getNaturalOrder); 48 49 if (!persistLogs) { 50 for (let i = messages.length - 1; i >= 0; i--) { 51 if (messages[i].type === MESSAGE_TYPE.CLEAR) { 52 return batchActions([ 53 messagesClear(), 54 { 55 type: MESSAGES_ADD, 56 messages: messages.slice(i), 57 }, 58 ]); 59 } 60 } 61 } 62 63 // When this is used for non-cached messages then handle clear message and 64 // split up into batches 65 return { 66 type: MESSAGES_ADD, 67 messages, 68 }; 69 } 70 71 function messagesClear() { 72 return { 73 type: MESSAGES_CLEAR, 74 }; 75 } 76 77 function messagesDisable(ids) { 78 return { 79 type: MESSAGES_DISABLE, 80 ids, 81 }; 82 } 83 84 function privateMessagesClear() { 85 return { 86 type: PRIVATE_MESSAGES_CLEAR, 87 }; 88 } 89 90 function targetMessagesRemove(targetFront) { 91 return { 92 type: TARGET_MESSAGES_REMOVE, 93 targetFront, 94 }; 95 } 96 97 function messageOpen(id) { 98 return { 99 type: MESSAGE_OPEN, 100 id, 101 }; 102 } 103 104 function messageClose(id) { 105 return { 106 type: MESSAGE_CLOSE, 107 id, 108 }; 109 } 110 111 /** 112 * Make a query on the server to get a list of DOM elements matching the given 113 * CSS selectors and store the information in the state. 114 * 115 * @param {Message} message 116 * The CSSWarning message 117 */ 118 function messageGetMatchingElements(message) { 119 return async ({ dispatch, commands }) => { 120 try { 121 // We need to do the querySelectorAll using the target the message is coming from, 122 // as well as with the window the warning message was emitted from. 123 const selectedTargetFront = message?.targetFront; 124 125 const response = await commands.scriptCommand.execute( 126 `document.querySelectorAll('${message.cssSelectors}')`, 127 { 128 selectedTargetFront, 129 innerWindowID: message.innerWindowID, 130 disableBreaks: true, 131 } 132 ); 133 dispatch({ 134 type: CSS_MESSAGE_ADD_MATCHING_ELEMENTS, 135 id: message.id, 136 elements: response.result, 137 }); 138 } catch (err) { 139 console.error(err); 140 } 141 }; 142 } 143 144 function messageRemove(id) { 145 return { 146 type: MESSAGE_REMOVE, 147 id, 148 }; 149 } 150 151 function networkMessageUpdates(packets) { 152 const messages = packets.map(packet => 153 prepareMessage(packet, defaultIdGenerator) 154 ); 155 156 return { 157 type: NETWORK_MESSAGES_UPDATE, 158 messages, 159 }; 160 } 161 162 function networkUpdateRequests(updates) { 163 return { 164 type: NETWORK_UPDATES_REQUEST, 165 updates, 166 }; 167 } 168 169 module.exports = { 170 messagesAdd, 171 messagesClear, 172 messagesDisable, 173 messageOpen, 174 messageClose, 175 messageRemove, 176 messageGetMatchingElements, 177 networkMessageUpdates, 178 networkUpdateRequests, 179 privateMessagesClear, 180 targetMessagesRemove, 181 };