Copy in contenteditable <span> - html

I need to copy content in span tag <span contenteditable="true">//content</span>
but it is not implemented in FireFox, is there any solutions for this?
and this is my span http://jsfiddle.net/watxD/

A. var Result = $('span[contenteditable="true"]').text();
B. var Result = $('span[contenteditable="true"]').html();
C. List of nodes inside: var Result = $('span[contenteditable="true"]').contents();
D. You can do it with Rangy Library http://code.google.com/p/rangy/. (It use native methods for FF, Chrome, Opera, IE9 and not-native for IE <= 8 )
Code below is using jQuery - but you can rewrite it on pure node Javascript
You need to create range for you node
var Range = rangy.createRange();
You need to select content of node
Range.selectNodeContents( $('span[contenteditable="true"]')[0] )
Represent result as you want:
var Result = Range.toString(); // Returns the text contained within the range.
var Result = Range.toHtml(); // Returns a string containing an HTML representation of the range.

There isn't pure JavaScript way to do this in Firefox without requiring the user to mess around with their preferences. Allowing general access to the system clipboard from JavaScript is dangerous, so the user may not want to do this. Here's an article that outlines the issues and how to enable clipboard access in Firefox. I don't know whether it's up to date.
http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard
The other option is a Flash-based hacky workaround such as ZeroClipboard.

Related

Canonical way to reference Elements

Is there a canonical way to set an ID—or other searchable, persistent data attribute—on Elements in Google Docs, such that I can easily refer back to it later? I'm looking for something equivalent to getElementById in javascript. Almost all examples I've seen, including Google's own docs, seem to reference objects by searching for text strings or inserting new strings.
I've found one reference in the NamedRanges class to a getId function, but I can't find any place to set that ID. I do see the setAttributes function on Elements but that seems to apply only for pre-defined attribute types. I haven't tested that, though.
In case it's relevant: my interest is in automatically creating a document from a Google Sheet and populating based on the current values in the sheet. I'd like to assign specific Elements individual IDs so I can easily retrieve the Element and replace the text if the values in the sheet change later on.
Turns out that this is possible using NamedRanges, I just didn't read carefully enough.
Note: All the following examples are working off this Google doc. You can make a copy and select "Script Editor" from the Tools menu to see the code.
You can assign named ranges pretty easily using Apps Script. The below code looks through the doc for [[TITLE]] and [[ABSTRACT]] and assigns named ranges to those chunks. Note that in the aforelinked doc I put them in a table to avoid issues with partial ranges.
function assignNamedRanges() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const placeholders = ["title", "abstract"];
placeholders.forEach(p => {
const rangeBuilder = doc.newRange();
const text = body.findText("[[" + p.toUpperCase() + "]]");
rangeBuilder.addElement(text.getElement());
doc.addNamedRange(p, rangeBuilder.build());
});
}
Once you assigned them, you can update the range to something else in a separate function:
function updateNamedRanges() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const title = doc.getNamedRanges("title")[0];
const abstract = doc.getNamedRanges("abstract")[0];
title.getRange().getRangeElements()[0].getElement().asText().setText("Bob");
abstract.getRange().getRangeElements()[0].getElement().asText().setText("I like pancakes");
}
Note that NamedRanges are persistent, and the multiple NamedRange instances can have the same name. This means that if you run the first function four times, you'll have eight named ranges. You can make a convenience function to clear all those out pretty easily:
function clearNamedRanges() {
DocumentApp.getActiveDocument().getNamedRanges().forEach(r => {
r.remove();
})
}
So I been checking the documentation about elements for Google Docs in AppScript and it seems that some of them can be modified but not as freely as it looks as noted in the documentation:
Elements shown in bold can be inserted; non-bold elements can only be manipulated in place.
I tried checking with setAttributes as you mentioned however the attributes itself can only be from a document elements like: TEXT, PARAGRAPH, TABLE, ETC, this elements can't receive an ID as there is not method to insert an specific ID as you are requiring, most of the values that can be inserted are specific element attributes like: Font size, Font family, etc.

Get A Google Doc Element By ID with Google App Script

Question:
I am trying to avoid looping through the document until I find the tag and then grabbing the parent. Does anyone know if there is a way to find a google doc element by an id or handle?
Context:
I have a script that opens a defined template doc; copies it into a new document element by element, and then replaces any mustache tags with the values from a passed in object using the replaceText function. This part works fine, but there is one section where I would like to repeat a table row based on the object value being an array. Unfortunately I can not seem to figure out how to locate the table in the template doc other than something like:
if('{{ some tag }}' in aElement){
var repeatableElement = aElement.getParent();
if(repeatableElement){
goRunRepeatFunction();
}
}
Just seems like there should be a way to jump directly to an element by id.
Your task can be accomplished by using this snippet:
function getElement() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var numOfElements = body.getNumChildren();
var element = body.getChild(INDEX_OF_THE_CHILD).asText();
console.log(element.getText());
}
This gathers the body of the document and retrieves the number of children it has. This number represents the elements the document has. So, in order to access a certain element, you just have to make use of the getChild(index) method
Note
Please bear in mind that the getNumChildren() method will end up returning the number of all the elements in the document, including empty lines - these will be considered elements as well.
Usually, when working with a document in Apps Script, the elements are retrieved by their types in order to preserve them. So for example, if you have images in your document, it'd be best to retrieve those by using the getImages() and this will end up returning a list of containing all the images in the document, with each image being accessible by their index.
Another method would be to retrieve all the paragraphs in the body by using the getParagraphs() method, but if you know the exact structure of your document, you can retrieve all the elements by their type, and then access them as such.
Reference
Class ContainerElement - getNumChildren();
Class ContainerElement - getChild(Integer);
Class Body - getParagraphs().

Chrome Driver: where is dynamic div element?

I'm trying to scrape the price from this page: https://www.samsclub.com/sams/mm-pinto-beans-12lb/prod21002291.ip?xid=plp:product:1:2
The price is in a div that I can see in the element using Dev Tools but it's not in the page source so my research tells me that it's probably a dynamically generated div element. More reading recommended Chrome Driver which returns the data with the dynamic div element but I'm finding it's not there. It's just like viewing page source from the browser.
ChromeDriver driver = new ChromeDriver(#"C:\chromedriver");
driver.Url = "https://www.samsclub.com/sams/mm-pinto-beans-12lb/prod21002291.ip?xid=plp:product:1:2";
driver.Navigate();
var source = driver.PageSource;
var pathElement = driver.FindElementById("Price-group");
Is there another step for ChromeDriver to retrun the data with the dynamic divs?
Thanks
Price-Group is not an id. it is a class. Try with FindElementByClassName. Hope that club value is selected before findind the price. Coz, I see price value is displayed only fater selecting club
var pathElement = driver.FindElementByClassName("Price-group");

ClosedXML - getting cell style after conditional format applied

When accessing a cell's style programmatically, as in
ws.Cell(4, 10).Style.Fill.BackgroundColor
the default style is returned. In this case, the cell has conditional formatting applied and contains a value such that the format is applied (gray fill). In an automated test, we would like to READ this applied format to verify it is as expected. Can this be done?
I found the list of conditional formats stored at the worksheet level.
var ws = workbook.Worksheet("[your sheet]");
var condformatlist = ws.RangeAddress.Worksheet.ConditionalFormats ;
If you know the range the format is applied to, you can find it in the list with linq.
var theconditionalformat =
ws.RangeAddress.Worksheet.ConditionalFormatsWhere(
x => x.Range == ws.Range("COPYRANGE")
);

Get page selection including HTML?

I'm writing a Chrome Extension, and I was wondering if it was possible to get the selected text of a particular tab, including the underlying HTML? So if I select a link, it should also return the <a> tag.
I tried looking at the context menu event objects (yes, I'm using a context menu for this), and this is all that comes with the callback:
editable : false
menuItemId : 1
pageUrl : <the URL>
selectionText : <the selected text in plaintext formatting, not HTML>
It also returns a Tab object, but nothing in there was very useful, either.
So I'm kind of at a loss here. Is this even possible? If so, any ideas you might have would be great. Thanks! :)
Getting the selected text of a page is fairly easy, you can do something like
var text = window.getSelection().toString();
and you'll get a text representation of the currently selected text that you can pass from a content script to a background page or a popup.
Getting HTML content is a lot more difficult, mostly because the selection isn't always at a clean HTML boundary in the document (what if you only select a small part of a long link, or a few cells of a table for example). The most direct way to get all of the html associated with a selection is to reference commonAncestorContainer, which is a property on a selection range that corresponds with the deepest node which contains both the start and end of the selection. To get this, you'd do something like:
var selection = window.getSelection();
// Only works with a single range - add extra logic to
// iterate over more ranges if needed
var range = selection.getRangeAt(0);
var container = range.commonAncestorContainer;
var html = container.innerHTML
Of course, this will likely contain a lot of HTML that wasn't actually selected. It's possible that you could iterate through the children of the common ancestor and prune out anything that wasn't in the selection, but that's going to be a bit more involved and may not be necessary depending on what you're trying to do.
To show how to wrap this all up into an extension, I've written a short sample which you can reference:
http://github.com/kurrik/chrome-extensions/tree/master/contentscript-selection/
If you don't want all of the siblings, just the selected HTML, use range's other methods like .cloneContents() (to copy) or .extractContents() (to cut).
Here I use .cloneContents():
function getSelectedHTML() {
var range = window.getSelection().getRangeAt(0); // Get the selected range
var div = document.createElement("div");
div.appendChild(range.cloneContents()); // Get the document fragment from selected range
return div.innerHTML; // Return the actual HTML
}