简体   繁体   English

在新窗口中打开XMLHttpRequest的responseText

[英]Open XMLHttpRequest's responseText in new window

Is there a way to open XMLHttpRequest's responseText as a working webpage in new window? 有没有办法在新窗口中将XMLHttpRequest的responseText打开为工作页面?

Details: I am building a chrome extension that looks for changes on websites by making an XMLHttpRequest and comparing responseText from last version (from background.js). 详细信息:我正在构建一个chrome扩展程序,该扩展程序通过发出XMLHttpRequest并比较最新版本(来自background.js)的responseText来查找网站上的更改。 After it finds a change, it notifies user through chrome notification. 找到更改后,它将通过chrome通知通知用户。 On clicking this notification, I want to open a tab with this exact data (xhttp.response) and original url, without making any new request to the website . 点击此通知后,我想打开一个包含确切数据(xhttp.response)和原始url的标签, 而无需对网站进行任何新请求 This is important for this extension to work properly on auction-related sites and during flash sales. 这对于此扩展在与拍卖相关的网站上以及在快速销售期间正常工作很重要。 Note that I need this application to run in the background, without even taking up the resources that chrome needs for rendering images/fonts (this is why I am not using chrome extension called Refresh Monkey ). 请注意,我需要此应用程序在后台运行,甚至不占用chrome渲染图像/字体所需的资源(这就是为什么我不使用称为Refresh Monkey的 chrome扩展名的原因)。 I have tried window.open('data:text/xml,' + xmlhttp.responseText), but it does not seem to do anything at all! 我已经尝试过window.open('data:text / xml,'+ xmlhttp.responseText),但它似乎根本没有做任何事情!

If there is no way to do this, then I will simply use window.open(url) to open the webpage in a new tab. 如果无法执行此操作,那么我将只使用window.open(url)在新选项卡中打开网页。 This will load the whole webpage again, is there anyway to make it load faster, because I just loaded this exact url on XMLHttpRequest. 这将再次加载整个网页,无论如何都可以使其加载更快,因为我只是在XMLHttpRequest上加载了这个确切的URL。

I would suggest you call chrome.tabs.create to create a new tab with a predefined local html page. 我建议您调用chrome.tabs.create来创建一个带有预定义本地html页面的新标签页。 In that html page, you could call chrome.runtime.sendMessage to notify background page that it has been created and wants to retrieve responseText, once received response, display it in the page. 在该html页面中,您可以调用chrome.runtime.sendMessage通知后台页面它已经创建并且想要检索responseText,一旦收到响应,就将其显示在页面中。

Sample code: 样例代码:

Background page: 背景页面:

chrome.tabs.create({ url: 'predefined.html' });

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'getText') {
    sendResponse({text: 'YOUR RESPONSE TEXT HERE'});
  }
});

predefined.html 预定义的.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <p id="textId"></p>
    <script src="predefined.js"></script>
</body>
</html>

predefined.js 预定义.js

chrome.runtime.sendMessage({action: 'getText'}, function(response) {
    document.getElementById('textId').innerText = response.text;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM