Browse Source

add canonical tag if not present (fix #360)

Former-commit-id: c615b206c247eb243e07b174c71805e6754b451e
Gildas 6 years ago
parent
commit
a505d08965

+ 1 - 0
extension/core/content/content-main.js

@@ -108,6 +108,7 @@ this.singlefile.extension.core.content.main = this.singlefile.extension.core.con
 		processor = new singlefile.lib.SingleFile(options);
 		const preInitializationPromises = [];
 		options.insertSingleFileComment = true;
+		options.insertCanonicalLink = true;
 		if (!options.saveRawPage) {
 			if (!options.removeFrames && frames && window.frames && window.frames.length) {
 				let frameTreePromise;

+ 1 - 0
lib/single-file/index.js

@@ -56,6 +56,7 @@ this.singlefile = this.singlefile || {
 			options.doc = doc;
 			options.win = win;
 			options.insertSingleFileComment = true;
+			options.insertCanonicalLink = true;
 			const singleFile = new this.SingleFile(options);
 			await singleFile.run();
 			return await singleFile.getPageData();

+ 13 - 0
lib/single-file/single-file-core.js

@@ -463,6 +463,17 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 					(infobarContent ? " \n info: " + infobarContent : "") + "\n");
 				this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
 			}
+			if (this.options.insertCanonicalLink) {
+				let canonicalLink = this.doc.querySelector("link[rel=canonical]");
+				if (!canonicalLink) {
+					canonicalLink = this.doc.createElement("link");
+					canonicalLink.setAttribute("rel", "canonical");
+					this.doc.head.appendChild(canonicalLink);
+				}
+				if (canonicalLink && !canonicalLink.href) {
+					canonicalLink.href = this.options.saveUrl;
+				}
+			}
 			let size;
 			if (this.options.displayStats) {
 				size = util.getContentSize(this.doc.documentElement.outerHTML);
@@ -958,6 +969,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 
 			async function initializeProcessor(frameData, frameElement, frameWindowId, batchRequest, options) {
 				options.insertSingleFileComment = false;
+				options.insertCanonicalLink = false;
 				options.saveFavicon = false;
 				options.doc = null;
 				options.win = null;
@@ -1045,6 +1057,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 				linkElement.removeAttribute("href");
 				const options = Object.create(this.options);
 				options.insertSingleFileComment = false;
+				options.insertCanonicalLink = false;
 				options.saveFavicon = false;
 				options.removeUnusedStyles = false;
 				options.removeAlternativeMedias = false;