Firefox addon: how to grab data from webpage? - firefox

Purpose
I am trying to make a common answer database for a website form. The form is a description of computer hardware specifications, and I am trying to make it so that based on a model field, other fields are filled in automatically. The extension with have two buttons, "populate" which will based on the model field, check the database for a matching entry, and then populate data into the forms based on that. The second button being "save", which will take data in the fields and marry it to the model field and place it into the database.
Question
So, my main question is on interacting with the webpage itself, how do I grab data from the webpage, and then how do I make changes to fields?

So, my main question is on interacting with the webpage itself, how do I grab data from the webpage, and then how do I make changes to fields?
You can do this with a Firefox Add-on SDK Page-mod, click here for the documentation
Here is an example for getting data:
Example Page-Mod
/lib/main.js:
var tag = "p";
var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "*.mozilla.org",
contentScriptFile: data.url("element-getter.js"),
onAttach: function(worker) {
worker.port.emit("getElements", tag);
worker.port.on("gotElement", function(elementContent) {
console.log(elementContent);
});
}
});
/data/element-getter.js:
self.port.on("getElements", function(tag) {
var elements = document.getElementsByTagName(tag);
for (var i = 0; i < elements.length; i++) {
self.port.emit("gotElement", elements[i].innerHTML);
}
});

Related

How to reload kendo grid with filtered data?

I have a kendo grid, in which I have selected filter on one column, I am reloading the grid, I want the same filter to be there when grid reloads.
I am using the below code to reload the grid. It works, but it doesn't show the selected filter item checked. I checked IDR in the filter then reloaded the page, it shows 1 item selected but doesn't show IDR as checked.
function ReloadGrid() {
var grid = $('#gridId').data('kendoGrid');
grid.dataSource.read();
grid.setDataSource(grid.dataSource);
}
Well there is two way to achieve this.
One way is to save filters in database and second one is to use local storage as mentioned in comment.
I prefer second one, using local storage to save filters and load it upon read.
#GaloisGirl is pointing you in right direction.
Check this example again: Persist state
Basic usage of locale storage is to save some data under some name (key,value):
let person = {
name: 'foo',
lastName: 'bar'
};
let save = function (person) {
let personString = JSON.stringify(person);
localStorage.setItem('person', personString);
console.log('Storing person: ', personString);
};
let load = function () {
let personString = localStorage.getItem('person'); // <----string
let person = JSON.parse(personString); // <----object
console.log('Stored person: ', person);
};
let remove = function (name) {
localStorage.removeItem(name);
console.log('Removed from local storage!');
};
save(person);
load();
remove(person.name);
In kendo world you need to save current options state of grid in local storage, you can achieve that by adding button like in example or on the fly with change or with window.onbeforeunload or like in your example beforen reload grid.
You can check saved data under application tab in browsers, eg. chrome:
Hope it helps, gl!

how to pass softcoded element to crm web resource script

I'm having trouble passing in the name of an element into a Dyanamics CRM web Resource javacript.
This code works:
function OnFormLoad()
{
var subGrid = window.parent.document.getElementById("Claims")
// do work
}
This code doesn't:
function OnFormLoad(GridName)
{
var subGrid = window.parent.document.getElementById(GridName)
// do work
}
How do I pass in the name of the element I want to work with?
Please refrain from using document.getElementById in Dynamics as it is not supported.
I believe you are trying to get GridContext and get Data from that Grid.
For Example on Account entity we have Contacts as Grid and then you wish to get data from that Grid.
I replicated the same on Account Entity (OnLoad) and get tried to get data from Contacts Grid.
When adding OnLoad event I have passed Grid name as Parameter as below.
I have added below onLoad Js on Account entity and was able to retrieve data from grid.
Note: I have added timeout because directly firing onload was not able to load complete page and then grid Name was not available.
function onLoad(executionContext,gridName){
setTimeout(function(){ getGridDatat(executionContext,gridName); }, 3000);
}
function getGridDatat(executionContext,gridName){
debugger
var formContext = executionContext.getFormContext();
var gridContext = formContext.getControl("Contacts"); // get the grid context
var myRows = gridContext.getGrid().getRows();
/*var myRow = myRows.get(arg);
var gridRowData = myRow.getData();*/
var firstRow =myRows.get(0).getData();
var firstRowAllAttributes = firstrow.entity.attributes.getAll()
var firstRowfirstAttributeValue = firstrow.entity.attributes.get(0).getValue()
}
If you want to perform some operation on change of data formGird then there is one more way to achieve this. Make your grid as Editable and then you can find Events for that gird as below and could perform your operations.

Populating a grid via autocomplete and posting the results

I have a view where I create a new company.
The company has a number of trades, or which 1 is a primary trade.
So when I enter the trades for that company, I select a trade via autocomplete, and this trade is added to a grid of trades underneath the autocomplete textbox. The grid contains the tradeId as a hidden field, the trade, and a radio button to indicate whether the trade is a primary trade and a remove button.
This is part of a form that contains other company details such as address.
Now I am wondering if I can use knockout and (maybe) jsrender to populate the grid without posting to the server?
When I have filled in the grid AND the other company details, I then want to submit the data to the controller post method.
Normally I use the Html helpers to post values to the controller, but I don't see how I can do that using knockout.
Yes you can use Knockout for this. If you have not checked the tutorials out yet then try this Knockout List and Collections tutorial. This should point you in the right direction. What you'll need to do is create a Trade object with observable properties and in a separate knockout view model create an observableArray to store trade objects. For information on posting to the server there are other tutorials in the same location.
function Trade(item) {
var self = this;
self.tradeId = ko.observable(item.tradeId);
self.tradeName = ko.observable(item.tradeName);
self.isPrimary = ko.observable(item.isPrimary);
}
function TradesViewModel() {
var self = this;
// Editable data
self.trades = ko.observableArray([]);
self.removeTrade = function(trade) { self.trades.remove(trades) }
self.save = function() {
$.post("/controller/action", self.trades);
}
}
ko.applyBindings(new TradesViewModel());

Defining which field was changed in a grid

Is it possible to identify on a kendo UI grid which field was altered on a row edit?
Right now I am sending the entire row to the server that was changed. I would like to send
the request to the server to also include a variable holding the name
of the field that was edited.
Is something like that supported by kendo or
is there a work around for that?
This is not supported out of the box. However the grid API should allow this to be implemented. Check the edit and save events. In there you can listen to changes of the model instance which is currently being edit. Here is a quick example:
$("#grid").kendoGrid({
edit: function(e) {
e.model.unbind("change", model_change).bind("change", model_change);
}
});
function model_change(e) {
var model = this;
var field = e.field;
// store somewhere the field and model
}

jqGrid Dynamic dropdownlist

I am new to jqGrid, and having trouble with achieving a couple of tasks. Any guidance will be a huge help.
I need to have dynamically populated drop down list.
$("#HEALTH_PLAN_CATEGORY_VALUE_ID").change(function () { $.getJSON('/Ajax/GetCountyForHealthPlan', { id: $(this).val() }, function (counties) { var ddlSelectedCounty = $("#COUNTY_GSA_LOOKUP_ID");
// clear all previous options $("#COUNTY_GSA_LOOKUP_ID > option").remove();
// populate the products for (i = 0; i < counties.length; i++) {
ddlSelectedCounty.append($("<option />").val(counties[i].Value).text(counties[i].Text)); }
});
});
Thanks a lot.
If you need to fill options in the select list of edit form for some column dynamically you should use dataUrl to get the data from the server and buildSelect to convert the server response to <select><option value='val'>Text</option>...</select>. See "UPDATED" part of the answer for an example.

Resources