Chrome extension - retrieving global variable from webpage

后端 未结 5 1756
离开以前
离开以前 2020-11-22 17:10

I am working on an extension for Chrome. I wish parse the content of the \"original\" Gmail message (the currently viewed message).

I tried to utilize the jQuery.loa

5条回答
  •  梦谈多话
    2020-11-22 17:48

    There is a new API for web pages to communicate securely and without any side effects (window.postMessage can have other listeners!) to the content script.

    "From the web page, use the runtime.sendMessage or runtime.connect APIs to send a message to a specific app or extension"

    // The ID of the extension we want to talk to.
    var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";
    
    // Make a simple request:
    chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
      function(response) {
        if (!response.success)
        handleError(url);
    });
    

    "From your app or extension, you may listen to messages from web pages via the runtime.onMessageExternal or runtime.onConnectExternal APIs, similar to cross-extension messaging. Only the web page can initiate a connection. [...]"

    (from http://developer.chrome.com/extensions/messaging.html) This is still only available in chrome's dev channel, but seems like it'll be in the next version or so.

    Don't ask me how this works, it seems highly confusing. How on earth does chrome.runtime get defined on the web page? What if the script already defined that variable for some reason? I also couldn't find the chromium bug report to see the history of the development of this feature.

提交回复
热议问题