🚧 Try to make user chosen extract

For #1
This commit is contained in:
Clement 2020-07-07 11:28:51 +02:00
parent b571af29c5
commit fad8af07a7
7 changed files with 103 additions and 11 deletions

View File

@ -11,7 +11,7 @@
"background": { "background": {
"scripts": ["scripts/background.js"] "scripts": ["scripts/background.js"]
}, },
"permissions": ["activeTab", "storage"], "permissions": ["activeTab", "storage", "contextMenus"],
"options_ui": { "options_ui": {
"page": "options.html" "page": "options.html"
}, },
@ -20,7 +20,7 @@
"matches": ["http://*/*", "https://*/*"], "matches": ["http://*/*", "https://*/*"],
"js": ["scripts/contentscript.js"], "js": ["scripts/contentscript.js"],
"run_at": "document_end", "run_at": "document_end",
"all_frames": false "all_frames": true
} }
], ],
"browser_action": { "browser_action": {

View File

@ -58,5 +58,9 @@
"excerpt": { "excerpt": {
"message": "Add content", "message": "Add content",
"description": "Option to add the webpage content into the email" "description": "Option to add the webpage content into the email"
},
"selectDomToSend": {
"message": "Select DOM To Send",
"description": "Select DOM To Send"
} }
} }

View File

@ -58,5 +58,9 @@
"excerpt": { "excerpt": {
"message": "Ajouter le contenu", "message": "Ajouter le contenu",
"description": "Option to add the webpage content into the email" "description": "Option to add the webpage content into the email"
},
"selectDomToSend": {
"message": "Partie à envoyer par email",
"description": "Select DOM To Send"
} }
} }

View File

@ -1,6 +1,46 @@
import ext from "./utils/ext"; import ext from "./utils/ext";
import storage from "./utils/storage"; import storage from "./utils/storage";
let onMenuAdded = function() {
if (ext.browser.runtime.lastError) {
console.log(`Error: ${ext.browser.runtime.lastError}`);
} else {
console.log("Readlater menu created successfully");
}
};
ext.contextMenus.create(
{
id: "dom-selection",
title: ext.i18n.getMessage("selectDomToSend"),
contexts: ["all"]
},
onMenuAdded
);
ext.contextMenus.onClicked.addListener(function(info, tab) {
if ("dom-selection" === info.menuItemId) {
var page_data = {
title: "",
description: "",
image: "",
comment: "",
url: document.location.href
};
ext.tabs.sendMessage(tab.id, { action: "process-page" }, function(data) {
ext.tabs.sendMessage(tab.id, { action: "DOM", data: data }, function(
response
) {
if (response && response.action === "saved") {
console.log(ext.i18n.getMessage("savedSuccessfully"));
} else {
console.log(response.error + " (" + response.status + ")");
}
});
});
}
});
ext.runtime.onMessage.addListener(function(request, sender, sendResponse) { ext.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === "perform-save") { if (request.action === "perform-save") {
var data = request.data; var data = request.data;

View File

@ -1,35 +1,67 @@
import ext from "./utils/ext"; import ext from "./utils/ext";
import storage from "./utils/storage";
var extractTags = () => { let extractDOM = () => {
var url = document.location.href; let indexInParent = node => {
let children = node.parentNode.childNodes;
let num = 0;
for (let i = 0; i < children.length; i++) {
if (children[i] === node) return num;
if (children[i].nodeType === 1) num++;
}
return -1;
};
let gx = el => {
let xp = "";
for (; el && el.nodeType === 1; el = el.parentNode) {
let id = indexInParent(el) + 1; // jQuery(el.parentNode).children(el.tagName).index(el)+1;
id = id > 1 ? "[" + id + "]" : "";
xp = "/" + el.tagName.toLowerCase() + id + xp;
}
return xp;
};
document.addEventListener("click", function(evt) {
let tar = evt.target;
while (tar && tar !== this) {
if (tar.matches(".shikihover")) {
let data = extractTags();
data.xpath = gx(tar);
}
}
});
};
let extractTags = () => {
let url = document.location.href;
if (!url || !url.match(/^http/)) { if (!url || !url.match(/^http/)) {
console.error("Invalid URL : " + url); console.error("Invalid URL : " + url);
return; return;
} }
var data = { let data = {
title: "", title: "",
description: "", description: "",
image: "", image: "",
url: url url: url
}; };
var ogTitle = document.querySelector("meta[property='og:title']"); let ogTitle = document.querySelector("meta[property='og:title']");
if (ogTitle) { if (ogTitle) {
data.title = ogTitle.getAttribute("content"); data.title = ogTitle.getAttribute("content");
} else { } else {
data.title = document.title; data.title = document.title;
} }
var descriptionTag = let descriptionTag =
document.querySelector("meta[property='og:description']") || document.querySelector("meta[property='og:description']") ||
document.querySelector("meta[name='description']"); document.querySelector("meta[name='description']");
if (descriptionTag) { if (descriptionTag) {
data.description = descriptionTag.getAttribute("content"); data.description = descriptionTag.getAttribute("content");
} }
var imgTag = let imgTag =
document.querySelector("meta[property='og:image']") || document.querySelector("meta[property='og:image']") ||
document.querySelector("meta[property='twitter-image']"); document.querySelector("meta[property='twitter-image']");
if (imgTag) { if (imgTag) {
@ -50,6 +82,10 @@ function onRequest(request, sender, sendResponse) {
if (request.action === "process-page") { if (request.action === "process-page") {
sendResponse(extractTags()); sendResponse(extractTags());
} }
if (request.action === "DOM") {
console.log("DOM");
sendResponse(extractDOM());
}
} }
ext.runtime.onMessage.addListener(onRequest); ext.runtime.onMessage.addListener(onRequest);

View File

@ -20,8 +20,7 @@ storage.get("extract", function(resp) {
}); });
var renderMessage = message => { var renderMessage = message => {
var displayContainer = document.getElementById("display-container"); document.getElementById("display-container").textContent = message;
displayContainer.textContent = message;
}; };
var renderBookmark = data => { var renderBookmark = data => {

View File

@ -0,0 +1,9 @@
div:hover,
section:hover {
border: 3px solid yellow;
}
div,
section {
border: none;
}