【问题标题】:programmatically click Gmail's "show original" button in a chrome extension?以编程方式在 Chrome 扩展程序中单击 Gmail 的“显示原始”按钮?
【发布时间】:2011-08-14 00:34:25
【问题描述】:

我似乎找不到在 chrome 扩展程序中以编程方式单击 gmail 的显示原始按钮的方法,源中似乎没有任何链接。

但是 url 类似于格式化的电子邮件,也许可以构造,除了它有某种我无法获得的用户 ID:

普通邮件查看:

https://mail.google.com/mail/?shva=1#inbox/131bfc47a65cb2fe

显示原文:

https://mail.google.com/mail/?ui=2&ik=8b4b18b93a&view=om&th=131bfc47a65cb2fe

记下用户 ID &ik=8b4b18b93a

是否有可能获得显示原始链接的链接?

谢谢

【问题讨论】:

  • 据我所知,th= 仍然无法被发现。该 URL 将显示线程中第一条消息的 ID,但不会显示其他消息的 ID。

标签: javascript hyperlink google-chrome-extension gmail


【解决方案1】:

当我在任何 gmail 页面上单击“查看页面源代码”时,我会在 var GLOBALS=[...] 数组中看到此键。我会使用 XMLHttpRequest 从后台页面读取 gmail 页面源代码,然后使用正则表达式对其进行解析以找到此键。

另一种方法是使用内容脚本将<script> 标记注入到gmail 页面,然后使用custom events 将此GLOBALS 数组传递回内容脚本(所有这些都是为了突破内容脚本沙箱)。

【讨论】:

    【解决方案2】:
    //Inject the following Script from contentScript to Page Script
    
    //Allow firing an event [http://stackoverflow.com/questions/2381572/how-can-i-trigger-a-javascript-event-click]
    
    function fireEvent(node, eventName) {
    // Make sure we use the ownerDocument from the provided node to avoid cross-window //problems
    var doc;
    if (node.ownerDocument) {
        doc = node.ownerDocument;
    } else if (node.nodeType == 9) {
        // the node may be the document itself, nodeType 9 = DOCUMENT_NODE
        doc = node;
    } else {
        throw new Error("Invalid node passed to JSUtil.fireEvent: " + node.id);
    }
    
    if (node.fireEvent) {
        // IE-style
        var event = doc.createEventObject();
        event.synthetic = true; // allow detection of synthetic events
        node.fireEvent("on" + eventName, event);
    } else if (node.dispatchEvent) {
        // Gecko-style approach is much more difficult.
        var eventClass = "";
    
        // Different events have different event classes.
        // If this switch statement can't map an eventName to an eventClass,
        // the event firing is going to fail.
        switch (eventName) {
            case "click":
                // Dispatching of 'click' appears to not work correctly in Safari. Use 'mousedown' or 'mouseup' instead.
            case "mousedown":
            case "mouseup":
                eventClass = "MouseEvents";
                break;
    
            case "focus":
            case "change":
            case "blur":
            case "select":
                eventClass = "HTMLEvents";
                break;
    
            default:
                throw "JSUtil.fireEvent: Couldn't find an event class for event '" + eventName + "'.";
                break;
        }
        var event = doc.createEvent(eventClass);
        var bubbles = eventName == "change" ? false : true;
        event.initEvent(eventName, bubbles, true); // All events created as bubbling and cancelable.
    
        event.synthetic = true; // allow detection of synthetic events
        node.dispatchEvent(event);
    }
    }
    
    //Then select the element
    var xx = document.query(showOriginalSelector);
    
    //Fire mouseUp and mouseDown Events simultaneously
    fireEvent(xx, 'mousedown');
    fireEvent(xx, 'mouseup');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 2013-06-27
      • 2023-04-09
      • 2019-01-20
      相关资源
      最近更新 更多