查看网页生成源的最佳方式?

发布于 2024-08-11 17:01:38 字数 820 浏览 15 评论 0原文

我正在寻找一个工具,它可以为我提供正确的生成源,包括由 AJAX 请求输入到 W3 验证器中所做的 DOM 更改。我尝试过以下方法:

  1. Web 开发人员工具栏< /a> - 根据文档类型生成无效源(例如,它删除标签的自关闭部分)。丢失页面的文档类型部分。
  2. Firebug - 修复源代码中的潜在缺陷(例如未封闭的标签)。还会丢失标签的 doctype 部分并注入本身是无效 HTML 的控制台。
  3. IE 开发工具栏 - 根据文档类型生成无效源(例如,它使所有标签大写,违反 XHTML 规范)。
  4. 突出显示 + 查看选择源 - 通常很难获取整个页面,也不包括文档类型。

是否有任何程序或附加组件可以为我提供源代码的确切当前版本,而无需以某种方式修复或更改它?到目前为止,Firebug 似乎是最好的,但我担心它可能会修复我的一些错误。

解决方案

事实证明,正如贾斯汀所解释的那样,我想要的东西没有确切的解决方案。最好的解决方案似乎是验证 Firebug 控制台内部的源代码,即使它会包含由 Firebug 引起的一些错误。我还要感谢 Forgotten Semicolon 解释了为什么“查看生成的源代码”与实际源代码不匹配。如果我可以标记 2 个最佳答案,我会的。

I'm looking for a tool that will give me the proper generated source including DOM changes made by AJAX requests for input into W3's validator. I've tried the following methods:

  1. Web Developer Toolbar - Generates invalid source according to the doc-type (e.g. it removes the self closing portion of tags). Loses the doctype portion of the page.
  2. Firebug - Fixes potential flaws in the source (e.g. unclosed tags). Also loses doctype portion of tags and injects the console which itself is invalid HTML.
  3. IE Developer Toolbar - Generates invalid source according to the doc-type (e.g. it makes all tags uppercase, against XHTML spec).
  4. Highlight + View Selection Source - Frequently difficult to get the entire page, also excludes doc-type.

Is there any program or add-on out there that will give me the exact current version of the source, without fixing or changing it in some way? So far, Firebug seems the best, but I worry it may fix some of my mistakes.

Solution

It turns out there is no exact solution to what I wanted as Justin explained. The best solution seems to be to validate the source inside of Firebug's console, even though it will contain some errors caused by Firebug. I'd also like to thank Forgotten Semicolon for explaining why "View Generated Source" doesn't match the actual source. If I could mark 2 best answers, I would.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

帅气称霸 2024-08-18 17:01:39

为什么不输入这是网址栏?

javascript:alert(document.body.innerHTML)

Why not type this is the urlbar?

javascript:alert(document.body.innerHTML)
灯角 2024-08-18 17:01:39

在 elements 选项卡中,右键单击 html 节点 >复制>复制元素 - 然后粘贴到编辑器中。

如上所述,一旦源代码被转换为 DOM 树,原始源代码就不再存在于浏览器中。您所做的任何更改都将针对 DOM,而不是源。

但是,您可以将修改后的 DOM 解析回 HTML,让您看到“生成的源”。

  1. 在 Chrome 中,打开开发人员工具并单击“元素”选项卡。
  2. 右键单击 HTML 元素。
  3. 选择复制>复制元素。
  4. 粘贴到编辑器中。

您现在可以看到当前 DOM 作为 HTML 页面。

这不是完整的 DOM

请注意,DOM 不能完全由 HTML 文档表示。这是因为 DOM 的属性比 HTML 的属性多得多。然而,这将完成合理的工作。

In the elements tab, right click the html node > copy > copy element - then paste into an editor.

As has been mentioned above, once the source has been converted into a DOM tree, the original source no longer exists in the browser. Any changes you make will be to the DOM, not the source.

However, you can parse the modified DOM back into HTML, letting you see the "generated source".

  1. In Chrome, open the developer tools and click the elements tab.
  2. Right click the HTML element.
  3. Choose copy > copy element.
  4. Paste into an editor.

You can now see the current DOM as an HTML page.

This is not the full DOM

Note that the DOM cannot be fully represented by an HTML document. This is because the DOM has many more properties than the HTML has attributes. However this will do a reasonable job.

南薇 2024-08-18 17:01:39

我认为 IE 开发工具(F12)有;查看>来源>> DOM(页面)

您需要复制并粘贴 DOM 并将其保存以发送到验证器。

I think IE dev tools (F12) has; View > Source > DOM (Page)

You would need to copy and paste the DOM and save it to send to the validator.

阳光下的泡沫是彩色的 2024-08-18 17:01:39

我唯一发现的是 Safari 的 BetterSource 扩展,这将向您显示文档的操纵源,唯一的缺点与 Firefox 完全不同

Only thing i found is the BetterSource extension for Safari this will show you the manipulated source of the document only downside is nothing remotely like it for Firefox

月下伊人醉 2024-08-18 17:01:39

下面的 javascript 代码片段将为您提供完整的 ajax 渲染 HTML 生成源。独立于浏览器的一种。享受 :)

function outerHTML(node){
    // if IE, Chrome take the internal method otherwise build one as lower versions of firefox
        //does not support element.outerHTML property
  return node.outerHTML || (
      function(n){
          var div = document.createElement('div'), h;
          div.appendChild( n.cloneNode(true) );
          h = div.innerHTML;
          div = null;
          return h;
      })(node);
  }


 var outerhtml = outerHTML(document.getElementsByTagName('html')[0]);
var node = document.doctype;
var doctypestring="";
if(node)
{
     // IE8 and below does not have document.doctype and you will get null if you access it.

 doctypestring = "<!DOCTYPE "
         + node.name
         + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
         + (!node.publicId && node.systemId ? ' SYSTEM' : '') 
         + (node.systemId ? ' "' + node.systemId + '"' : '')
         + '>';
         }
         else

         {

             // for IE8 and below you can access doctype like this

         doctypestring = document.all[0].text;
         }
doctypestring +outerhtml ;

The below javascript code snippet will get you the complete ajax rendered HTML generated source. Browser independent one. Enjoy :)

function outerHTML(node){
    // if IE, Chrome take the internal method otherwise build one as lower versions of firefox
        //does not support element.outerHTML property
  return node.outerHTML || (
      function(n){
          var div = document.createElement('div'), h;
          div.appendChild( n.cloneNode(true) );
          h = div.innerHTML;
          div = null;
          return h;
      })(node);
  }


 var outerhtml = outerHTML(document.getElementsByTagName('html')[0]);
var node = document.doctype;
var doctypestring="";
if(node)
{
     // IE8 and below does not have document.doctype and you will get null if you access it.

 doctypestring = "<!DOCTYPE "
         + node.name
         + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
         + (!node.publicId && node.systemId ? ' SYSTEM' : '') 
         + (node.systemId ? ' "' + node.systemId + '"' : '')
         + '>';
         }
         else

         {

             // for IE8 and below you can access doctype like this

         doctypestring = document.all[0].text;
         }
doctypestring +outerhtml ;
心意如水 2024-08-18 17:01:39

我能够通过将 ajax 调用的结果记录到控制台来解决类似的问题。这是返回的 html,我可以很容易地看到它存在的任何问题。

在我的ajax调用的.done()函数中,我添加了console.log(results),这样我就可以在调试器控制台中看到html。

function GetReversals() {
    $("#getReversalsLoadingButton").removeClass("d-none");
    $("#getReversalsButton").addClass("d-none");

    $.ajax({
        url: '/Home/LookupReversals',
        data: $("#LookupReversals").serialize(),
        type: 'Post',
        cache: false
    }).done(function (result) {
        $('#reversalResults').html(result);
        console.log(result);
    }).fail(function (jqXHR, textStatus, errorThrown) {
        //alert("There was a problem getting results.  Please try again. " + jqXHR.responseText + " | " + jqXHR.statusText);
        $("#reversalResults").html("<div class='text-danger'>" + jqXHR.responseText + "</div>");
    }).always(function () {
        $("#getReversalsLoadingButton").addClass("d-none");
        $("#getReversalsButton").removeClass("d-none");
    });
}

I was able to solve a similar issue by logging the results of the ajax call to the console. This was the html returned and I could easily see any issues that it had.

in my .done() function of my ajax call I added console.log(results) so I could see the html in the debugger console.

function GetReversals() {
    $("#getReversalsLoadingButton").removeClass("d-none");
    $("#getReversalsButton").addClass("d-none");

    $.ajax({
        url: '/Home/LookupReversals',
        data: $("#LookupReversals").serialize(),
        type: 'Post',
        cache: false
    }).done(function (result) {
        $('#reversalResults').html(result);
        console.log(result);
    }).fail(function (jqXHR, textStatus, errorThrown) {
        //alert("There was a problem getting results.  Please try again. " + jqXHR.responseText + " | " + jqXHR.statusText);
        $("#reversalResults").html("<div class='text-danger'>" + jqXHR.responseText + "</div>");
    }).always(function () {
        $("#getReversalsLoadingButton").addClass("d-none");
        $("#getReversalsButton").removeClass("d-none");
    });
}

柏拉图鍀咏恒 2024-08-18 17:01:38

贾斯汀已经死定了。这里的关键点是 HTML 只是一种用于描述文档的语言。一旦浏览器读取它,它就消失了。开放标签、关闭标签和格式都由解析器处理然后消失。任何显示 HTML 的工具都是根据文档内容生成的,因此它始终有效。

有一次我不得不向另一位网络开发人员解释这一点,他花了一段时间才接受。

您可以在任何 JavaScript 控制台中亲自尝试:

el = document.createElement('div');
el.innerHTML = "<p>Some text<P>More text";
el.innerHTML; // <p>Some text</p><p>More text</p>

未闭合的标签和大写标签名称消失了,因为该 HTML 在第二行之后被解析并丢弃。

从 JavaScript 修改文档的正确方法是使用 document 方法(createElementappendChildsetAttribute 等)。 )并且您会发现这些函数中都没有引用标签或 HTML 语法。如果您使用 document.writeinnerHTML 或其他 HTML 调用来修改您的页面,验证它的唯一方法是捕获您所输入的内容进入它们并单独验证 HTML。

也就是说,获取文档的 HTML 表示形式的最简单方法是:

document.documentElement.innerHTML

Justin is dead on. The key point here is that HTML is just a language for describing a document. Once the browser reads it, it's gone. Open tags, close tags, and formatting are all taken care of by the parser and then go away. Any tool that shows you HTML is generating it based on the contents of the document, so it will always be valid.

I had to explain this to another web developer once, and it took a little while for him to accept it.

You can try it for yourself in any JavaScript console:

el = document.createElement('div');
el.innerHTML = "<p>Some text<P>More text";
el.innerHTML; // <p>Some text</p><p>More text</p>

The un-closed tags and uppercase tag names are gone, because that HTML was parsed and discarded after the second line.

The right way to modify the document from JavaScript is with document methods (createElement, appendChild, setAttribute, etc.) and you'll observe that there's no reference to tags or HTML syntax in any of those functions. If you're using document.write, innerHTML, or other HTML-speaking calls to modify your pages, the only way to validate it is to catch what you're putting into them and validate that HTML separately.

That said, the simplest way to get at the HTML representation of the document is this:

document.documentElement.innerHTML
岁月染过的梦 2024-08-18 17:01:38

[更新以响应已编辑问题中的更多详细信息]

您遇到的问题是,一旦页面被ajax请求修改,当前的HTML仅存在于浏览器的DOM内部 -​​ 有除了可以从 DOM 中提取的内容之外,不再可以验证任何独立的源 HTML。

正如您所观察到的,IE 的 DOM 以大写形式存储标签,修复未封闭的标签,并对最初获得的 HTML 进行许多其他更改。这是因为浏览器通常非常擅长获取有问题的 HTML(例如未封闭的标签)并修复这些问题以显示对用户有用的内容。据我所知,一旦 HTML 被 IE 规范化,从 DOM 的角度来看,原始的 HTML 源代码基本上就丢失了。

Firefox 很可能做出的这些更改较少,因此 Firebug 可能是您更好的选择。

最后一个(也是更费力的)选项可能适用于具有简单 ajax 更改的页面,例如从服务器获取一些 HTML 并将其导入到特定元素内的页面中。在这种情况下,您可以使用 fiddler 或类似工具手动将原始 HTML 与 Ajax HTML 拼接在一起。这可能比它的价值更麻烦,并且容易出错,但它又是一种可能性。

[对原始问题的原始回复]

Fiddler (http://www.fiddlertool.com/ )是一个免费的、独立于浏览器的工具,它可以很好地获取浏览器接收到的准确 HTML。它向您显示线路上的确切字节以及解码/解压缩等内容,您可以将这些内容输入到任何 HTML 分析工具中。它还显示标头、计时、HTTP 状态和许多其他好东西。

如果您想测试服务器如何响应略有不同的标头,您还可以使用 fiddler 复制和重建请求。

Fiddler 充当代理服务器,位于浏览器和网站之间,并记录双向流量。

[updating in response to more details in the edited question]

The problem you're running into is that, once a page is modified by ajax requests, the current HTML exists only inside the browser's DOM-- there's no longer any independent source HTML that you can validate other than what you can pull out of the DOM.

As you've observed, IE's DOM stores tags in upper case, fixes up unclosed tags, and makes lots of other alterations to the HTML it got originally. This is because browsers are generally very good at taking HTML with problems (e.g. unclosed tags) and fixing up those problems to display something useful to the user. Once the HTML has been canonicalized by IE, the original source HTML is essentially lost from the DOM's perspective, as far as I know.

Firefox most likley makes fewer of these changes, so Firebug is probably your better bet.

A final (and more labor-intensive) option may work for pages with simple ajax alterations, e.g. fetching some HTML from the server and importing this into the page inside a particular element. In that case, you can use fiddler or similar tool to manually stitch together the original HTML with the Ajax HTML. This is probably more trouble than it's worth, and is error prone, but it's one more possibility.

[Original response here to the original question]

Fiddler (http://www.fiddlertool.com/) is a free, browser-independent tool which works very well to fetch the exact HTML received by a browser. It shows you exact bytes on the wire as well as decoded/unzipped/etc content which you can feed into any HTML analysis tool. It also shows headers, timings, HTTP status, and lots of other good stuff.

You can also use fiddler to copy and rebuild requests if you want to test how a server responds to slightly different headers.

Fiddler works as a proxy server, sitting in between your browser and the website, and logs traffic going both ways.

又怨 2024-08-18 17:01:38

我知道这是一篇旧帖子,但我刚刚发现了这个一块金子。这是旧的(2006 年),但仍然适用于 IE9。我亲自为此添加了一个书签。

只需将其复制粘贴到浏览器的地址栏中:

javascript:void(window.open("javascript:document.open(\"text/plain\");document.write(opener.document.body.parentNode.outerHTML)"))

对于 Firefox,Web 开发人员工具栏即可完成此工作。我通常使用这个,但有时,一些肮脏的第 3 方 asp.net 控件会根据用户代理生成不同的标记...

编辑

正如 Bryan 在评论中指出的那样,某些浏览器删除了 javascript : 复制/粘贴到网址栏中时的部分。我刚刚测试了一下,IE10也是这样。

I know this is an old post, but I just found this piece of gold. This is old (2006), but still works with IE9. I personnally added a bookmark with this.

Just copy paste this in your browser's address bar:

javascript:void(window.open("javascript:document.open(\"text/plain\");document.write(opener.document.body.parentNode.outerHTML)"))

As for firefox, web developper tool bar does the job. I usually use this, but sometimes, some dirty 3rd party asp.net controls generates differents markups based on the user agent...

EDIT

As Bryan pointed in the comment, some browser remove the javascript: part when copy/pasting in url bar. I just tested and that's the case with IE10.

野稚 2024-08-18 17:01:38

如果您在 Chrome 中加载文档,Developer|Elements 视图将向您显示由 JS 代码修改的 HTML。它不是直接的 HTML 文本,您必须打开(展开)任何感兴趣的元素,但您可以有效地检查生成的 HTML。

If you load the document in Chrome, the Developer|Elements view will show you the HTML as fiddled by your JS code. It's not directly HTML text and you have to open (unfold) any elements of interest, but you effectively get to inspect the generated HTML.

凉薄对峙 2024-08-18 17:01:38

在 Web 开发人员工具栏中,您是否尝试过 Tools ->验证 HTML工具 ->验证本地 HTML 选项?

验证 HTML 选项将 url 发送到验证器,这对于面向公众的网站效果很好。 验证本地 HTML 选项将当前页面的 HTML 发送到验证器,这对于登录后的页面或不可公开访问的页面非常有效。

您可能还想尝试查看源图表(也如FireFox 插件)。那里有一个有趣的注释:

问。为什么查看源图表将我的 XHTML 标记更改为 HTML 标记?

A.事实并非如此。浏览器正在进行这些更改,VSC 仅显示浏览器对您的代码所做的操作。最常见的是:自闭合标签失去了结束斜杠 (/)。 有关详细信息,请参阅这篇有关渲染源的文章 (archive.org)

In the Web Developer Toolbar, have you tried the Tools -> Validate HTML or Tools -> Validate Local HTML options?

The Validate HTML option sends the url to the validator, which works well with publicly facing sites. The Validate Local HTML option sends the current page's HTML to the validator, which works well with pages behind a login, or those that aren't publicly accessible.

You may also want to try View Source Chart (also as FireFox add-on). An interesting note there:

Q. Why does View Source Chart change my XHTML tags to HTML tags?

A. It doesn't. The browser is making these changes, VSC merely displays what the browser has done with your code. Most common: self closing tags lose their closing slash (/). See this article on Rendered Source for more information (archive.org).

╰沐子 2024-08-18 17:01:38

使用 Firefox Web 开发人员工具栏 (https://addons.mozilla.org/en- US/firefox/addon/60)

只需转到查看源代码 ->查看生成的源代码

我一直使用它来完成完全相同的事情。

Using the Firefox Web Developer Toolbar (https://addons.mozilla.org/en-US/firefox/addon/60)

Just go to View Source -> View Generated Source

I use it all the time for the exact same thing.

少女七分熟 2024-08-18 17:01:38

我遇到了同样的问题,我在这里找到了解决方案:

http://ubuntuincident.wordpress.com/2011/04/15/scraping-ajax-web-pages/

因此,要使用 Crowbar,这里的工具:

http://simile.mit.edu/wiki/Crowbar(现 (2015-12) 404s)
回程机链接:
http://web.archive.org/web /20140421160451/http://simile.mit.edu/wiki/Crowbar

它给了我错误的、无效的 HTML。

I had the same problem, and I've found here a solution:

http://ubuntuincident.wordpress.com/2011/04/15/scraping-ajax-web-pages/

So, to use Crowbar, the tool from here:

http://simile.mit.edu/wiki/Crowbar (now (2015-12) 404s)
wayback machine link:
http://web.archive.org/web/20140421160451/http://simile.mit.edu/wiki/Crowbar

It gave me the faulty, invalid HTML.

九公里浅绿 2024-08-18 17:01:38

这是一个老问题,这是一个老答案,已经曾经对我完美地工作了很多年,但现在不再这样了,至少从 2016 年 1 月开始是这样:

SquareFree 的“生成的源代码”书签完全符合您的要求,并且与 @Johnny5 的其他精美“旧金”不同,它显示为源代码(而不是由浏览器正常呈现,至少在以下情况下) Mac 上的 Google Chrome):

https://www.squarefree.com/bookmarklets/webdevel。 html# generated_source

不幸的是,它的行为就像@Johnny5 中的“旧黄金”:它不再显示为源代码。对不起。

This is an old question, and here's an old answer that has once worked flawlessly for me for many years, but doesn't any more, at least not as of January 2016:

The "Generated Source" bookmarklet from SquareFree does exactly what you want -- and, unlike the otherwise fine "old gold" from @Johnny5, displays as source code (rather than being rendered normally by the browser, at least in the case of Google Chrome on Mac):

https://www.squarefree.com/bookmarklets/webdevel.html#generated_source

Unfortunately, it behaves just like the "old gold" from @Johnny5: it does not show up as source code any more. Sorry.

听闻余生 2024-08-18 17:01:38

在 Firefox 中,只需按 ctrl-a(选择屏幕上的所有内容),然后右键单击“查看选择源”。这会捕获 JavaScript 对 DOM 所做的任何更改。

In Firefox, just ctrl-a (select everything on the screen) then right click "View Selection Source". This captures any changes made by JavaScript to the DOM.

绝影如岚 2024-08-18 17:01:38

警报(文档.documentElement.outerHTML);

alert(document.documentElement.outerHTML);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文