I'm trying to run an IMacros macro written in javascript on a webpage, like so:
我正在尝试在网页上运行用javascript编写的IMacros宏,如下所示:
for (var i = 1; i < 18; i++) {
document.querySelector(".foo table > tbody > tr:nth-child(" + i + ") > .goo:nth-child(2) > a").click();
document.querySelector(".foo > a").click();
if (i % 17===0) {
alert('Reset i');
i = 1;
}
}
Everything seems to work fine from the js console, but when I run the macro, I get:
从js控制台看,一切似乎都运行良好,但是当我运行宏时,我得到:
"ReferenceError: document is not defined, line 2 (Error code: -991)"
I've loaded JQuery into iMacros with this, and put my code between:
我用这个将JQuery加载到iMacros中,并将我的代码放在:
$(document).ready(function () {
//
});
But I keep getting this error if I use JQuery:
但是如果我使用JQuery,我会一直收到这个错误:
TypeError: $ is not a function, line 28 (Error code: -991)
And if I only use JS, I get the same 'document is not defined' error as before.
如果我只使用JS,我会像以前一样得到“文档未定义”错误。
So my question is, do I need to define the document, and how do I do it?
所以我的问题是,我是否需要定义文档,我该怎么做?
1 个解决方案
#1
6
I've never been able to load jQuery into an imacros script, but that's not such a big deal in the end. To access the DOM you need to refer each element as: window.content.document.getElementsByClassName('foo')
for example. This will give you an array so be sure to pick each of the elements in the array you need:
我从来没有能够将jQuery加载到imacros脚本中,但这最终并不是什么大问题。要访问DOM,您需要将每个元素引用为:window.content.document.getElementsByClassName('foo')。这将为您提供一个数组,因此请务必选择所需数组中的每个元素:
var foo_class = window.content.document.getElementsByClassName('foo');
for (i=0;i<foo_class.length;i++){
//do something
}
Hope it helps
希望能帮助到你
EDIT to add working example:
编辑添加工作示例:
var links = window.content.document.getElementsByClassName('question-hyperlink');
var list=[]
for (i=0;i<links.length;i++){
txt=links[i].innerHTML;
list.push(txt);
}
number=links.length;
linkstexts=list.toString();
showme="number of links with class=question-hyperlink: "+number+" text links with class=question-hyperlink: "+linkstexts;
iimDisplay((showme))
Copy the code in a macro.js and run it in firefox on stackexchage. It will count all the links with the class="question-hyperlink" and will display their respective text - you can see it in the green textbox under Play(Loop) button.
复制宏.js中的代码并在stackexchage上的firefox中运行它。它将使用class =“question-hyperlink”计算所有链接并显示其各自的文本 - 您可以在Play(循环)按钮下的绿色文本框中看到它。
#1
6
I've never been able to load jQuery into an imacros script, but that's not such a big deal in the end. To access the DOM you need to refer each element as: window.content.document.getElementsByClassName('foo')
for example. This will give you an array so be sure to pick each of the elements in the array you need:
我从来没有能够将jQuery加载到imacros脚本中,但这最终并不是什么大问题。要访问DOM,您需要将每个元素引用为:window.content.document.getElementsByClassName('foo')。这将为您提供一个数组,因此请务必选择所需数组中的每个元素:
var foo_class = window.content.document.getElementsByClassName('foo');
for (i=0;i<foo_class.length;i++){
//do something
}
Hope it helps
希望能帮助到你
EDIT to add working example:
编辑添加工作示例:
var links = window.content.document.getElementsByClassName('question-hyperlink');
var list=[]
for (i=0;i<links.length;i++){
txt=links[i].innerHTML;
list.push(txt);
}
number=links.length;
linkstexts=list.toString();
showme="number of links with class=question-hyperlink: "+number+" text links with class=question-hyperlink: "+linkstexts;
iimDisplay((showme))
Copy the code in a macro.js and run it in firefox on stackexchage. It will count all the links with the class="question-hyperlink" and will display their respective text - you can see it in the green textbox under Play(Loop) button.
复制宏.js中的代码并在stackexchage上的firefox中运行它。它将使用class =“question-hyperlink”计算所有链接并显示其各自的文本 - 您可以在Play(循环)按钮下的绿色文本框中看到它。