programmatically click Gmail's "show original" button in a chrome extension?
I can't seem to find a way to click gmail's show original button programmatically in a chrome extension, ther开发者_StackOverflowe doesn't appear to be any link in the source.
However the url is similar to the formatted email and perhaps could be constructed, except it has some kind of user id which I have no way of obtaining:
regular mail view:
https://mail.google.com/mail/?shva=1#inbox/131bfc47a65cb2fe
show original:
https://mail.google.com/mail/?ui=2&ik=8b4b18b93a&view=om&th=131bfc47a65cb2fe
note the user id &ik=8b4b18b93a
is it possible to get a link to show original?
thanks
When I click "View page source" on any gmail page I see this key inside var GLOBALS=[...]
array. I would read gmail page source from a background page using XMLHttpRequest
and then parse it with regular expressions to find this key.
Another way would be to inject <script>
tag to gmail page using a content script, and then pass this GLOBALS
array back to a content script using custom events (all this to break out of content script sandbox).
//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');
精彩评论