Here’s another example of transferring data from a webpage to an Omni document. Omni Automation enables the contents to be extracted from a list on the webpage into the current OmniOutliner document:
Here are some example webpages that contain lists and data that can be transferred to an open OmniOutliner document using the techniques shown on this page.
This is the main JavaScript function, embedded in the webpage, or linked from a file (see bottom of this page), that performs the following:
(line 2) Retrieves the contents of the targeted HTML list
(line 3) Converts the resulting array into a JSON object string
(line 4) Encodes the JSON object string
(line 6) Replaces a placeholder in an encoded Omni Automation script (line 5) with the encoded JSON data
(line 7) Executes the encoded Omni Automation script
function addToOutline(listID){
var listItems = getListItemStrings(listID);
var listItemsString = JSON.stringify(listItems);
var encodedListItemsString = encodeURIComponent(listItemsString);
var OmniOutlinerScript = 'omnioutliner:///omnijs-run?script=var%20itemArray%20%3D%20XXXXX%0Avar%20nodes%20%3D%20document%2Eeditors%5B0%5D%2EselectedNodes%0Aif%28nodes%2Elength%20%3D%3D%201%29%7B%0A%09var%20alert%20%3D%20new%20Alert%28%22Item%20Placement%22%2C%20%22Should%20the%20list%20items%20be%20added%20as%20children%20or%20siblings%20of%20the%20selected%20outline%20item%3F%22%29%0A%09alert%2EaddOption%28%22Children%22%29%0A%09alert%2EaddOption%28%22Siblings%22%29%0A%09alert%2EaddOption%28%22Cancel%22%29%0A%09alert%2Eshow%28function%28result%29%7B%0A%09%09var%20selectedItem%20%3D%20nodes%5B0%5D%2Eobject%0A%09%09if%20%28result%20%3D%3D%200%29%7B%0A%09%09%09for%28i%20%3D%200%3B%20i%20%3C%20itemArray%2Elength%3B%20i%2B%2B%29%7B%0A%09%09%09%09selectedItem%2EaddChild%28selectedItem%2Eend%2Cfunction%28item%29%7Bitem%2Etopic%20%3D%20itemArray%5Bi%5D%7D%29%0A%09%09%09%7D%0A%09%09%7D%20else%20if%20%28result%20%3D%3D%201%29%7B%0A%09%09%09parentItem%20%3D%20selectedItem%2Eparent%0A%09%09%09for%28i%20%3D%200%3B%20i%20%3C%20itemArray%2Elength%3B%20i%2B%2B%29%7B%0A%09%09%09%09parentItem%2EaddChild%28parentItem%2Eend%2Cfunction%28item%29%7Bitem%2Etopic%20%3D%20itemArray%5Bi%5D%7D%29%0A%09%09%09%7D%0A%09%09%7D%20else%20%7B%0A%09%09%09throw%20new%20Error%28%27script%20cancelled%27%29%0A%09%09%7D%0A%09%7D%29%0A%7D%20else%20%7B%0A%09var%20alert%20%3D%20new%20Alert%28%27SELECTION%20ERROR%27%2C%27Please%20select%20a%20single%20outline%20item%20at%20which%20you%20wish%20to%20add%20items%2E%27%29%0A%09alert%2Eshow%28function%28result%29%7B%7D%29%0A%7D';
OmniOutlinerScript = OmniOutlinerScript.replace('XXXXX',encodedListItemsString);
window.location = OmniOutlinerScript;
}
The first step in transferring the contents of an HTML list to an OmniOutliner document is to create a JavaScript array containing the text of the list items. This is accomplished using this JavaScript function placed in the host webpage.
function getListItemStrings(listID){
var ul = document.getElementById(listID);
var items = ul.getElementsByTagName("li");
var listStrings = new Array();
for (var i = 0; i < items.length; ++i) {
listStrings.push(items[i].textContent);
};
return listStrings;
}
Getting List Items
01
functiongetListItemStrings(listID){
02
varul = document.getElementById(listID);
03
varitems = ul.getElementsByTagName("li");
04
varlistStrings = newArray();
05
for (var i = 0; i < items.length; ++i) {
06
listStrings.push(items[i].textContent);
07
};
08
returnlistStrings;
09
}
When this function is called by the main function, it is passed the CSS id of the list whose contents are to be retrieved.
function getListItemStrings(listID){
var ul = document.getElementById(listID);
var items = ul.getElementsByTagName("li");
var listStrings = items.map(function(item){
return item.textContent;
};
return listStrings;
}
Getting List Items (alternate)
01
functiongetListItemStrings(listID){
02
varul = document.getElementById(listID);
03
varitems = ul.getElementsByTagName("li");
04
varlistStrings = items.map(function(item){
05
returnitem.textContent;
06
};
07
returnlistStrings;
08
}
The Omni Automation Script
Here is the Omni Automation script that does the work of adding the retrieved list items into the currently open OmniOutliner document. In use, the script is encoded and the encoded script is placed within the main JavaScript function embedded in the webpage. Note the placeholder XXXXX (line 1) that is replaced with an array of the list item strings before the script is executed by the main JavaScript function.
var itemArray = XXXXX
var nodes = document.editors[0].selectedNodes
if(nodes.length == 1){
var alert = new Alert("Item Placement", "Should the list items be added as children or siblings of the selected outline item?")
alert.addOption("Children")
alert.addOption("Siblings")
alert.addOption("Cancel")
alert.show(function(result){
var selectedItem = nodes[0].object
if (result == 0){
for(i = 0; i < itemArray.length; i++){
selectedItem.addChild(selectedItem.end,function(item){item.topic = itemArray[i]})
}
} else if (result == 1){
parentItem = selectedItem.parent
for(i = 0; i < itemArray.length; i++){
parentItem.addChild(parentItem.end,function(item){item.topic = itemArray[i]})
}
} else {
throw new Error('script cancelled')
}
})
} else {
var alert = new Alert('SELECTION ERROR','Please select a single outline item at which you wish to add items.')
alert.show(function(result){})
}
Add List Items to Outline
01
varitemArray = XXXXX
02
varnodes = document.editors[0].selectedNodes
03
if(nodes.length == 1){
04
varalert = newAlert("Item Placement", "Should the list items be added as children or siblings of the selected outline item?")
varalert = newAlert('SELECTION ERROR','Please select a single outline item at which you wish to add items.')
25
alert.show(function(result){})
26
}
Adding Functions from File
You can incude the functionality shown on this webpage in your webpages by downloading the addListToOutline.js file and including this link in the HEAD section of your webpages. Add the TAP|CLICK triggers to your lists as shown above.