Skip to content

Commit

Permalink
Version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
welbert committed Nov 27, 2016
1 parent d1af0a9 commit e4c14b9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 6
}
4 changes: 0 additions & 4 deletions data/lib/jquery.min.js

This file was deleted.

91 changes: 75 additions & 16 deletions data/scripts/context-script.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,86 @@
self.on("click", function(node, data) {
self.postMessage(node, data);
self.postMessage(findCssSelector(node));
});

jQuery.fn.getPath = function() {
if (this.length != 1) throw 'Requires one element.';
findCssSelector = function(ele) {
ele = getRootBindingParent(ele);
let document = ele.ownerDocument;
if (!document || !document.contains(ele)) {
throw new Error("findCssSelector received element not inside document");
}

var path, node = this;
while (node.length) {
var realNode = node[0],
name = realNode.localName;
if (!name) break;
name = name.toLowerCase();
// document.querySelectorAll("#id") returns multiple if elements share an ID
if (ele.id &&
document.querySelectorAll("#" + CSS.escape(ele.id)).length === 1) {
return "#" + CSS.escape(ele.id);
}

var parent = node.parent();
// Inherently unique by tag name
let tagName = ele.localName;
if (tagName === "html") {
return "html";
}
if (tagName === "head") {
return "head";
}
if (tagName === "body") {
return "body";
}

var siblings = parent.children(name);
if (siblings.length > 1) {
name += ':eq(' + siblings.index(realNode) + ')';
// We might be able to find a unique class name
let selector, index, matches;
if (ele.classList.length > 0) {
for (let i = 0; i < ele.classList.length; i++) {
// Is this className unique by itself?
selector = "." + CSS.escape(ele.classList.item(i));
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
// Maybe it's unique with a tag name?
selector = tagName + selector;
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
// Maybe it's unique using a tag name and nth-child
index = positionInNodeList(ele, ele.parentNode.children) + 1;
selector = selector + ":nth-child(" + index + ")";
matches = document.querySelectorAll(selector);
if (matches.length === 1) {
return selector;
}
}
}

path = name + (path ? '>' + path : '');
node = parent;
// Not unique enough yet. As long as it's not a child of the document,
// continue recursing up until it is unique enough.
if (ele.parentNode !== document) {
index = positionInNodeList(ele, ele.parentNode.children) + 1;
selector = findCssSelector(ele.parentNode) + " > " +
tagName + ":nth-child(" + index + ")";
}

return path;
return selector;
};

function positionInNodeList(element, nodeList) {
for (let i = 0; i < nodeList.length; i++) {
if (element === nodeList[i]) {
return i;
}
}
return -1;
}

function getRootBindingParent(node) {
let parent;
let doc = node.ownerDocument;
if (!doc) {
return node;
}
//while ((parent = doc.getBindingParent(node))) {
// node = parent;
//}
return node;
}
4 changes: 2 additions & 2 deletions data/scripts/popup-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ self.port.on("show", function onShow() {
textArea.focus();
});

self.port.on("text-received", function onShow() {
textArea.value += "Write some text";
self.port.on("text-received", function onShow(text) {
textArea.value += text;
});
21 changes: 9 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
//-- EVENT OF ADDON ELEMENT

function onbuttonGuideAutomatorClicked(state) {
//tabs.open("https://www.npmjs.com/package/guide-automator");
popupGuideAutomator.show();
tabs.open("https://www.npmjs.com/package/guide-automator");
//popupGuideAutomator.show();
}

var ContextScript = 'self.on("click", function (node, data) {' +
' self.postMessage(node.value,data);' +
'});';

function onContextMessage(node, data) {
console.log(node);
function onContextMessage(cssSelector) {
clipboard.set(cssSelector);
popupGuideAutomator.port.emit("text-received", cssSelector);
}

//-- END EVENT OF ADDON ELEMENT
Expand All @@ -20,7 +17,7 @@ var data = require("sdk/self").data;
var buttons = require('sdk/ui/button/action');
var tabs = require("sdk/tabs");
var contextMenu = require("sdk/context-menu");

var clipboard = require("sdk/clipboard");
var popupGuideAutomator = require("sdk/panel").Panel({
contentURL: data.url("main.html"),
contentScriptFile: data.url("scripts/popup-middleware.js")
Expand All @@ -45,19 +42,19 @@ var menuItem = contextMenu.Item({
image: data.url("icon-16.png"),
context: contextMenu.SelectorContext("*"),
onMessage: onContextMessage,
contentScriptFile: [data.url("lib/jquery.min.js"), data.url("scripts/context-script.js")]
contentScriptFile: data.url("scripts/context-script.js")
});

//-- END ADDON ELEMENT

//-- popupGuideAutomator Comunication
popupGuideAutomator.on("show", function() {
popupGuideAutomator.port.emit("text-received");
popupGuideAutomator.port.emit("text-received", "teste");
popupGuideAutomator.port.emit("show");
});

popupGuideAutomator.port.on("text-entered", function(text) {
console.log(text);
console.log("Index-Port: " + text);
popupGuideAutomator.hide();
});
//-- END popupGuideAutomator Comunication
Expand Down

0 comments on commit e4c14b9

Please sign in to comment.