0

基本上,我制作了一个 javascript 来刷新页面,它会找到价格并在商品价格上涨时购买所需的价格。

我让它在没有 iframe 的情况下工作,但我需要在 iframe 中工作,这是我遇到的问题。

如果您访问此页面:[ http://m.roblox.com/items/100933289/privatesales ]

并运行此代码:

alert(document.getElementsByClassName('currency-robux')[0].innerHTML);

您会收到最低价格的提醒。在代码中,这不起作用(因此,我的问题。)

尝试在此页面上运行以下代码以使其正常工作 [ http://www.roblox.com/Junk-Bot-item?id=100933289 ]

var filePath = document.URL;
var itemid = filePath.slice(((filePath.search("="))+1));
var mobileRoot = 'http://m.roblox.com/items/';
var mobileEnd = '/privatesales';
var mobileFilePath = mobileRoot+itemid+mobileEnd;

var iframe2 = '<iframe id="frame" width="100%" height="1" scrolling="yes"></iframe>';
document.write(iframe2);
var iframe = parent.document.getElementById("frame");
iframe.height = 300;
iframe.width = 500;
iframe.src = mobileFilePath;
var price;
var snipe = false;
var lp = Number(prompt("Snipe Price?"));
document.title = "Sniping";

function takeOutCommas(s){
    var str = s;
    while ((str.indexOf(",")) !== -1){
        str = str.replace(",","");
    }
    return str;
}

function load() {
    if (snipe == false) {
        tgs = iframe.contentDocument.getElementsByClassName('currency-robux');
        price = Number((takeOutCommas(tgs[0].innerHTML)));
        alert(price);
    }
}

iframe.onload = load;
4

2 回答 2

0

您可以尝试同时拥有两个页面 - 来自“m.roblox.com”的页面和来自“www.roblox.com”的页面 - 在头顶添加以下内容:

<script>
  document.domain = "roblox.com";
</script>

不允许来自不同域的代码查看彼此的页面内容,但是如果您将域设置为相同的后缀,那么它应该可以工作。

于 2012-12-23T18:18:09.530 回答
0

如果您无法通过共享相同的document.domain="roblox.com"代码使其工作,那么您可以尝试将消息发布到 iframe。

把它放在 iframe 页面中:

window.addEventListener('message',function(e) {
});

在父页面中执行此操作以将消息(可以是字符串或对象,实际上是任何东西)传递给 iframe:

document.getElementById("frame").contentWindow.postMessage({ "json_example": true }, "*");

将其放在父级中以收听消息:

window.addEventListener("message", messageReceived, false);
function messageReceived(e) {
}

从 iframe 内部发布一条消息:

window.parent.postMessage('Hello Parent Page','*');
于 2012-12-23T20:16:36.923 回答