実現したいこと
下記のような、Chrome用のブックマークレットを作成しています。
- 表示中のページの完全なソースコードを取得する
- ソースコードを文字列として変数「HTMLsrc」に格納し、後半のスクリプトで作成したフォームから文法チェッカーのサイトへ送信する
前提
仕事で以上のようなブックマークレットを作成しているのですが、何故か正しく取得できているはずのソースコードが、正しく文法チェッカーに送信できないという問題が発生しています。
ブックマークレットそのものは最後まで実行されているためエラーコードなどは表示されていないようなのですが、いざ文法チェッカーで結果が表示されるとDoctypeもHTMLタグも、Bodyタグの中身も反映されていない様子です。
スクリプトに何か問題があるようでしたら、ご教示いただけないでしょうか。
発生している問題・エラーメッセージ
※エラーコードは表示されていないので、代わりに文法チェッカーの結果をコピーしました。
- Warning: Consider adding a lang attribute to the html start tag to declare the language of this document.
For further guidance, consult Declaring the overall language of a page and Choosing language tags.
If the HTML checker has misidentified the language of this document, please file an issue report or send e-mail to report the problem.
2. Error: The character encoding was not declared. Proceeding using windows-1252.
3. Error: End of file seen without seeing a doctype first. Expected <!DOCTYPE html>.
4. Error: Element head is missing a required instance of child element title.
Content model for element head:
If the document is an iframe srcdoc document or if title information is available from a higher-level protocol: Zero or more elements of metadata content, of which no more than one is a title element and no more than one is a base element.
Otherwise: One or more elements of metadata content, of which exactly one is a title element and no more than one is a base element.
Document checking completed.
該当のソースコード
JavaScript
1javascript:var HTMLsrc = new XMLSerializer().serializeToString(document.doctype) + document.getElementsByTagName('html')[0].outerHTML;d=document.createElement("form");d.method="POST";d.action="https://validator.w3.org/nu/";d.enctype="multipart/form-data";d.target="_blank";d.acceptCharset="utf-8";document.body.appendChild(d);i = document.createElement('input');i.setAttribute('type', 'hidden');i.setAttribute('value', HTMLsrc);d.appendChild(i);d.submit();
試したこと
そもそもソースコードが正しく取得できていないのではないかと考え確認のために以下のようなブックマークレットを作成・実行してみましたが、問題なく表示されました。
JavaScript
1javascript:let HTMLsrc = new XMLSerializer().serializeToString(document.doctype) + document.getElementsByTagName('html')[0].outerHTML;alert(HTMLsrc);
よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/02/21 03:13
2023/02/21 03:29
2023/02/21 03:37