Execute function on webpage? - javascript

Say a webpage has a function like:
function abc (){
return 'abc';
}
How would I execute and receive the return value of that function from my extension? I've tried this, no dice:
var s = getBrowser.contentWindow.abc();
Thanks in advance!

I think that if you want to do this without major security holes, you need to be using XPCSafeJSObjectWrapper, but I'm having trouble finding good documentation on it. https://developer.mozilla.org/en/XPConnect_wrappers has a little bit. (It might be that it happens automatically when doing the above, but I'm not sure.)

I think I've solved my own problem with:
getBrowser().contentWindow.wrappedJSObject.funcFromPage();

Related

JavaScript function not working with onclick

Here is the problem(if you can call it so), every time i write JS that includes functions it doesn't work. I know it's related to spaces, line-breaks or sth like that. Maybe I don't know the rule or the syntax. i'm using sublime text3(if it's related). And i will be thankful if you explain the reason to me so i won't be facing this problem ever again. here is a code that doesn't work on my computer
<script type="text/javascript">
document.getElementById("circle").onclick = function() {
document.getElementById("circle").style.display = "none";
}
</script>
Are you sure that the element with the ID "circle" has the onclick function as one of its properties? you can check the property list with the dir function that is on the console.
console.dir(document.getElementById("circle"));
The dir is not for production though. It does come in handy in development and debugging.
MDN Console.dir()

replace with regex then undo

I have a rather unique problem where I'm trying to run some jquery logic to replace text temporarily on a page. Then run some logic (I take a screenshot for a tool I'm using). So far this works great, the problem is that due to legacy code I need to revert the changes the replace call did on the page.
Any ideas on the best way to do this?
For the curious, I currently have:
$('body').html($('body').html().replace(/[a-zA-Z0-9\\*]*#.*\\.com/g,'[replaced for screenshot]'))
Thanks!
I'd question your motives and reasoning, but I'll provided an answer nonetheless:
var backup_body_html = $('body').html();
$('body').html(backup_body_html.replace(/[a-zA-Z0-9\\*]*#.*\\.com/g,'[replaced for screenshot]'));
Afterwards:
$('body').html(backup_body_html);
(Unless you need to keep hold of event handlers etc, in which case cloning is needed)
Cloning method:
var body_children = $("body").clone(true,true).children();
//other stuff (i.e. replacements)
//then:
$("body").html("");
$("body").append(body_children);
Not completely serious but I have to...
location.reload();
Before
var bodyHTML = document.body.innerHTML;
$('body').html(bodyHTML.replace(/[a-zA-Z0-9\\*]*#.*\\.com/g,'[replaced for screenshot]'));
After
$('body').html(bodyHTML)

Javascript - GetColoumnValue - IE6 problem

I have a js function named "GetListColumnValue". This function causes some problems with IE6. Is there any way to avoid the problem? (I thnk the problem is occured because of the concat) Here is the code sample. The last line is my solution which I am not sure that it works well. Any suggestions? Thanks.
function GetListColumnValue(listName, columnName) {
return document.getElementById(listName + "_" + columnName).value;
}
var DISCOUNT_QUANTITY = GetListColumnValue("lstRecords", "DISCOUNT_QUANTITY");
var DISCOUNT_QUANTITY = document.getElementById("lstRecords_DISCOUNT_QUANTITY");
IE6 has many many problems, but simple JS string concat isn't one of them. I don't think that's your problem.
You didn't specify what exactly the problem is, but looking at the two code samples you provided, they will do different things:
The first one (ie the function) returns the object.value, whereas the second one (ie setting it directly), you've just returned the object.
So the two code blocks set DISCOUNT_QUANTITY to different things. If you remove the .value from the function, it should work exactly the same as the other code block.
Hope that helps.

How to increase speed of getElementById() function on IE or give other solutions?

I have a project using Javascript parse json string and put data into div content.
In this case, all of itemname variables is the same div's id.
If variable i about 900, I run this code in Firefox 3 for <10ms, but it run on IE 7 for >9s, IE process this code slower 100 times than Firefox
I don't know what happen with IE ?
If I remove the line document.getElementById(itemname), speed of them seems the same.
The main problem arcording to me is document.getElementById() function?
Could you show me how to solve this prolem to increase this code on IE ?
Thank in advance.
var i = obj.items.length-2;
hnxmessageid = obj.items[i+1].v;
do{
itemname = obj.items[i].n;
itemvalue = obj.items[i].v;
document.getElementByid(itemname);
i--;
}while(i>=0);
Are you really noticing any latency?
gEBI is natively very very fast, I don't think you can avoid it anyway for what you're doing. Could you provide a low-down of what you're doing precisely? It looks like you're using a loop, but can you post exactly what you're doing inside of the loop, what your common goal of the script is?
document.getElementByid(itemname) is the fastest way to get a single element from the DOM in any real application you will sould not see any problems with using it, if you do see a problem you need to rethink you code a little it possible to acomplish any task with just a handfull of calls for this method. You can present you full problem if you like so I could show you an example
At least cache the reference to document:
var doc = document;
for(;;) {
doc.getElementById(..);
}

How to set a javascript variable or reuse one from another function

I have a problem going on here and without going into a lot of detail and confusing everyone, let me just ask the simple question.
Here are two functions. I need to use the "id" variable in the SubmitForm() function as well. Can someone please tell me how to do this? This is how new I am to js. :)
Thanks for the help.
AC.chooseFunc = function(id,label)
{
document.qSearch.action = ".index.php?dc=2&id="+ id;
//document.qSearch.action = "index.php?dc=2";
document.qSearch.submit();
}
*** This one fails.
function SubmitForm(id)
{
document.qSearch.action = "index.php?dc=2&id="+ id;
document.qSearch.submit()
}
What I need is the "id" var appended to the query string in the SubmitForm Function. Can someone tell me how to do this please? Thanks for the help!
Can this not be done??
First, as a disclaimer, I think we would all be able to give you better answers if you post an example page demonstrating how the document is put together.
Here's one way you can make it work. At or near the top of your script (or <script> tag) declare a variable:
var storedId;
Then, at the top of AC.chooseFunc, copy the value of id to storedId:
AC.chooseFunc = function(id,label)
{
storedId = id;
...
Finally, remove id from SubmitForm's parameters and use storedId instead:
function SubmitForm()
{
document.qSearch.action = "index.php?dc=2&id="+ storedId;
document.qSearch.submit();
}
i dont know if you would want to do this, but you could make the variable global in other words declare it outside of both functions.
Could you not just use a hidden input and have PHP add it to the query string?
Is id null? Check the value of id...
alert(id);
The second function looks OK to me. The problem could be your passing null into it.
it's pretty clear that the two functions aren't being called in the same way.
As mentioned in another comment, alert the value to see what you're really getting in the function.
The function itself looks fine, so the only conclusion is that they are not working from the same data OR they are called in different ways.
I'd recommend posting a little more of the code in the call tree

Categories

Resources