Chrome扩展程序 - 检索Gmail的原始邮件 [英] Chrome extension - retrieving Gmail's original message

查看:150
本文介绍了Chrome扩展程序 - 检索Gmail的原始邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Chrome扩展程序。我希望解析原始Gmail邮件的内容(当前查看的邮件)。



我熟悉以编程方式点击Gmail的显示原始 Chrome扩展程序中的按钮?。但是由于它没有显示很多信息,所以很难理解。



我试图利用jQuery.load()如下

  $(windows).load(function(){alert(GLOBALS);}); 

并将其放在内容脚本中,但它也不起作用。我正在使用Chrome的开发人员工具,它会在调用 alert(GLOBALS))时返回以下错误:

 未捕获参考错误:GLOBALS未定义

尽管使用开发者工具的控制台,键入控制台 GLOBALS 它返回一个数组。任何线索如何编程读取原始信息的内容?或者如何从内容脚本访问GLOBALS?

解决方案

内容脚本在孤立的环境中运行。要访问任何全局属性(页面的窗口),您必须注入新的< script> 元素,或使用事件侦听器传递数据。



请参阅< script> 元素的这个答案 -chrome-extension-with-youtube-events p>

示例



contentscript.js run_at document_end在清单中):

  var s = document.createElement('script'); 
s.src = chrome.extension.getURL('script.js');
(document.head || document.documentElement).appendChild(s);
s.onload = function(){
s.remove();
};

//事件监听器
document.addEventListener('RW759_connectExtension',function(e){
// e.detail包含传输的数据(可以是任何东西,范围
//从JavaScript对象到字符串)
//执行某些操作,例如:
alert(e.detail);
});

script.js - 位于扩展目录中,这将被注入页面本身:

  setTimeout(function(){
/ *示例:将数据从页面发送到Chrome扩展名* /
document.dispatchEvent(new CustomEvent('RW759_connectExtension',{
detail:GLOBALS //来自Gmail的一些变量
}));
},0);

由于此文件正在通过Chrome扩展名加载:DOM内的URL,脚本。 js必须添加到清单文件的web_accessible_resources部分。否则Chrome将拒绝加载脚本文件。



您应该在网页中运行尽可能少的逻辑,并处理内容脚本中的大部分逻辑。这有多个原因。首先,页面中注入的任何脚本都与网页运行在相同的上下文中,因此网页可以(故意或无意间)修改JavaScript / DOM方法,使您的扩展名停止工作。其次,内容脚本可以访问额外的功能,例如chrome。* API和跨原始网络请求的有限子集(只要扩展名已经声明了这些权限)。


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

I am familiar with programmatically click Gmail's "show original" button in a chrome extension?. But since it does not reveal much information, it is hard to understand it.

I tried to utilize the jQuery.load() as follows

$(windows).load(function() { alert(GLOBALS); });

and place it at the content script, but it does not work either. I am using Chrome's developer tools, which returns the following error on the invocation of the alert(GLOBALS);

Uncaught ReferenceError: GLOBALS is not defined

although, when using the developers tools' console, typing into the console GLOBALS it returns an array. Any clue how can to programatically read the content of the original message? or how to access the GLOBALS from the content script?

解决方案

Content scripts run in an isolated environment. To get access to the any global properties (of the page's window), you have to either inject a new <script> element, or use event listeners for passing data.

See this answer for example on injecting a <script> element in the context of the page.

Example

contentscript.js ("run_at": "document_end" in manifest):

var s = document.createElement('script');
s.src = chrome.extension.getURL('script.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
    s.remove();
};

// Event listener
document.addEventListener('RW759_connectExtension', function(e) {
    // e.detail contains the transferred data (can be anything, ranging
    // from JavaScript objects to strings).
    // Do something, for example:
    alert(e.detail);
});

script.js - Located in the extension directory, this will be injected into the page itself:

setTimeout(function() {
    /* Example: Send data from the page to your Chrome extension */
    document.dispatchEvent(new CustomEvent('RW759_connectExtension', {
        detail: GLOBALS // Some variable from Gmail.
    }));
}, 0);

Since this file is being loaded via a chrome-extension: URL from within the DOM, "script.js" must be added to the web_accessible_resources section of the manifest file. Otherwise Chrome will refuse to load the script file.

You should run as little logic as possible in the web page, and handle most of your logic in the content script. This has multiple reasons. First and foremost, any script injected in the page runs in the same context as the web page, so the web page can (deliberately or inadvertently) modify JavaScript/DOM methods in such a way that your extension stops working. Secondly, content script have access to extra features, such a limited subset of the chrome.* APIs and cross-origin network requests (provided that the extension has declared permissions for those).

这篇关于Chrome扩展程序 - 检索Gmail的原始邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆