Send DOM node object via chrome.tabs.sendMessage

后端 未结 2 1906
轮回少年
轮回少年 2020-12-20 07:43

I am writing a Chrome extension. I need to pass an element object from the content script to the background script.

The goal:
The extension is about record and r

相关标签:
2条回答
  • 2020-12-20 07:59

    You can't send a DOM element as a runtime.sendMessage() message

    The message in runtime.sendMessage() must be "a JSON-ifiable object". DOM elements/nodes are not JSON-ifiable. Thus, you can not send them. In your case, you are trying to send the target of the click event.

    What you will need to do instead of trying to serialize the DOM element is, ultimately, determined by why you need this information in your background script.

    If you want to identify the element, you will need to determine a unique selector. One way to do this would be to assign a unique ID to the element and pass that ID in your message. However, that will only be effective if you are wanting to refer to the DOM node during the time that page is loaded within that tab. Obviously, any ID you assign will not be available once the browser has left the page, or loaded it in a different tab. Thus, that alternative is only viable for identifying an element for the life of the current page. However, for an application where you were wanting to just store the actual DOM element, assigning a unique ID would be a valid solution. In other words, storing the DOM element would only be valid for the life of the page, so assigning a unique ID would be valid for the same time period (life of the current page).

    If you want methods which will uniquely identify the element when the page is re-loaded, you will need to use a different method than assigning an ID. What to use will depend largely on how you are going about selecting the element when you are wanting to use it and how resilient you want the selection to be with respect to changes in the page structure (e.g. on pages where the structure is dynamic, you may need to use other methods than would work on a static page).

    For your application, where you want to record and playback user actions, you will need to determine if you want to record these actions based on where the mouse is within the page, or based on the elements upon which the user initiates events. This is a common problem for applications/languages which are used to record/playback/simulate user actions. Commonly, the user is given the option as to how they want such user interaction to be recorded (e.g. by location or element). If you choose to store the user actions only by the location of the mouse at the time an event occurred, then you can use Document.elementFromPoint() to determine which element is now at that point and send the event to that element. However, when doing so, you will also need to track the scrolling state of the document and determine if you are going to store the location of the mouse based on the location of the mouse within the current display, or relative to the document.

    0 讨论(0)
  • 2020-12-20 08:21

    I used a workaround to click the element,

    besides save the element and then using element.click() I used the cords to click the element without saving the element itself:

    document.elementFromPoint(cords.x - window.pageXOffset, cords.y - window.pageYOffset).click();
    
    0 讨论(0)
提交回复
热议问题