-4

我正在 GMail 中编辑邮件。我想按下其中一个样式按钮,例如“缩进”、“缩进”、“从右到左”等。

我不想通过按下来做到这一点,而是通过在地址栏中键入一段 javascript 并按下回车键。

(我有兴趣这样做,所以我可以编写一个使用键盘而不是鼠标按下按钮的脚本。)

这可能吗?

赏金:

赏金将颁发给提供完整、有效的解决方案的人,我可以将其放入 Chrome 的地址栏中以使按钮(如“缩进”、“链接”、“外缩”、“从右到左”等)被按下。(当然,每个按钮的线路不同。)

4

4 回答 4

4

要选择元素,您首先需要选择 iframe

var inlineFrame = document.getElementById("canvas_frame");

接下来,您可以从 iframe 获取 contentwindow(iframe 的窗口范围),如下所示:

var contentwindow = inlineFrame.contentWindow;

接下来使用 queryselector(仅限真实浏览器(不支持 IE8)),您可以像这样选择按钮。([command='+bold']简单的意思是:任何带有属性命令的元素,其值为 '+bold',就像在 CSS 中一样)。

var button = contentwindow.document.querySelector("[command='+bold']");

如果您检查相关按钮,它们都包含您可以用来选择它们的命令属性。接下来,您可以随心所欲地使用按钮(在 stackoverflow 上寻找其他答案以了解模拟事件的不同方法,因为您应该能够自己做到这一点)。

如果你想把它作为一个书签,你当然也可以用它做一行,但我认为这样作为答案会更清楚。

于 2012-07-05T09:28:42.483 回答
1

首先,你需要弄清楚你想要被“点击”的 DOM 元素的 ID/标签索引/css 类,然后使用选择器访问它,最后调用它的点击事件。

要动态选择一个 DOM 元素,你可以使用document.getElementById(<id>)如果你知道它的 ID,然后document.getElementsByTagName(<tagName>)[<index>]如果你知道它的标签名称和索引。

如果您只知道元素使用特定的 css 类,您仍然可以循环每个标签,直到element.className==<className>或动态导入 jquery 然后调用$('.<className>').trigger('click');

希望这可以帮助

于 2012-07-10T08:37:30.620 回答
1

试试看Ctrl+BCtrl+ICtrl+U 这个意思吗?

于 2012-07-02T10:03:38.033 回答
0

Disclaimer: More or a generic answer, not directly related to Gmail.

To click any element on a page, you can use the element.click() method. Please note that all elements do not have the click method. <input> elements support though.

I'm not sure if you can access the document object from the URL bar. So if I were using Firefox, I would open Firebg or better, the Scratch Pad (Tools > Web Developer > Scratch Pad) and write something like this:-

javascript:function foo(){document.getElementById('link1').click();}foo();

Upon executing the above code, it will invoke the click method on the element with the ID 'link1'.

于 2012-07-02T10:34:24.450 回答