Reply to topic

[Solved] KHTMLPart executeScript() infinite loop

sebaro
Registered Member
Posts
18
Karma
0
OS
Hi,
I want to execute external scripts in websites.
The folowing does the job:
Code: Select all
...
m_part = dynamic_cast<KHTMLPart *>(parent);
connect(m_part,SIGNAL(completed()),this,SLOT(slotApplyScript()));
...
void slotApplyScript()
{
    QTextStream in(&jsFile);
    while (!in.atEnd()) {
     QString line = in.readLine();
     scriptContent += line + "\n";
    }
   
    if (!scriptContent.isEmpty()) {
      m_part->executeScript(scriptContent);
    }
}

but, if the script contains non empty "src" or "data" attributes, I'm getting a infinite loop.
How can I solve this?
Thanks.

Last edited by sebaro on Tue Jun 21, 2011 7:20 am, edited 1 time in total.
User avatar bcooksley
Administrator
Posts
18562
Karma
83
OS
Have you tried attaching a debugger to Konqueror and generating a backtrace? It might be a bug in KHTML.


System Settings and Device Actions KCM maintainer
Image
sebaro
Registered Member
Posts
18
Karma
0
OS
bcooksley wrote:Have you tried attaching a debugger to Konqueror and generating a backtrace? It might be a bug in KHTML.

No.

For a script like:
Code: Select all
document.getElementById('someElID').innerHTML = '<object data="someDataPath"></object>';

KHTMLPart loads the page, then it seems like is loading "someDataPath" like a new document too, so it executes the script, therefore the infinite loop.

Adding:
Code: Select all
std::cout << qPrintable(scriptContent) << "\n" << std::endl;

it prints the script conntent indefinitely.

I know very little qt/kde programming, I modified someone's konqueror plugin so I can execute scripts from a directory in some web pages I browse.
Maybe it's the way KHTMLPart works and there's more to be done.
Thanks!
User avatar bcooksley
Administrator
Posts
18562
Karma
83
OS
I would recommend sending an email to kde-devel@kde.org regarding this, as the developers of KHTML are likely going to know more about this issue..


System Settings and Device Actions KCM maintainer
Image
sebaro
Registered Member
Posts
18
Karma
0
OS
bcooksley wrote:I would recommend sending an email to kde-devel@kde.org regarding this, as the developers of KHTML are likely going to know more about this issue..

I did send an email to kde-devel. They said I should use KHTMLPart::parentPart(), but this is for frames in an HTML frameset. They also said I should contact kfm-devel from now on. I did. Here, Michael Pyne asked me if I use the -ffast-math gcc flag, which I don't, and to use a JS debugger. What's to debug in this line?
Code: Select all
document.getElementById('someElID').innerHTML = '<object data="someDataPath"></object>';


Now I found there's Userscript (Greasemonkey like) for Konqueror 4
http://websvn.kde.org/trunk/playground/utils/khtml_userscript/ but with the same problem. Unfortunatly I don't think the project is maintain anymore. The last revision is from 13 months ago.

I don't know who should I contact now.

Thanks.
User avatar dpalacio
Registered Member
Posts
237
Karma
2
OS
Have you considered a code refactor where you disconnect slotApplyScript once it is executed?


// Debian Sid amd64 KDE 4.6.5.
// Debian Squeeze i386. KDE 4.4.5.
connect(post,SIGNAL(readSignature()),qapp,SLOT(quit()));
sebaro
Registered Member
Posts
18
Karma
0
OS
dpalacio wrote:Have you considered a code refactor where you disconnect slotApplyScript once it is executed?

It never gets disconnected. If I disconnect it, the script is applied once when opening an new tab/window and is not applied on refresh or when entering a new webpage in the same tab/window.
GTalbot
Registered Member
Posts
69
Karma
1
OS
sebaro wrote:(...) and to use a JS debugger. What's to debug in this line?
Code: Select all
document.getElementById('someElID').innerHTML = '<object data="someDataPath"></object>';

(...)


Sebaro,

1- I would avoid innerHTML for many reasons. If you absolutely need to embed an <object> dynamically, then this can be done without innerHTML and with createElement("object") and DOM 2 HTML http://www.w3.org/TR/DOM-Level-2-HTML/h ... ID-9893177

2- I also would set the type (content-type, mimeType) of the object.

3- You could do smaller tests involving the dynamic attachment of an <object> into such someElID. <object> can be almost anything: an <img>, an application, a multi-media content (video, audio, whatever) an HTML fragment, or HTML document. That reinforces the necessity to declare a type.

4- Your someElID must be a block-level box container (like <p> or <div>) since <object> is an inline-block replaced element.

5- In your code, you were also missing escaping the forward slash into the closing tag:

Writing HTML in a SCRIPT Element
http://htmlhelp.com/tools/validator/pro ... .en#script

states
"
A common error (and the most common source of erroneous bug reports for the WDG HTML Validator) occurs when writing HTML tags within a SCRIPT element:
"

6- if you want to execute external scripts, then why not just create a script object and then specify the src attribute accordingly?

I can not really help you with the rest of your code, namely your slotApplyScript() function or as to why there is an infinite loop but I know DHTML and your DHTML code is not ideal.

regards, Gérard


sebaro
Registered Member
Posts
18
Karma
0
OS
Gérard, thank you for your suggestions.

You are right, setting the mimetype is a must.
Not setting it, KHTMLPart script thinks it's a html document and runs the script again, because it's a new KHTMLPart, generating the loop.
I want to embed multimedia files only, and setting the mimetype solves the problem.

Code: Select all
var bd = document.body;
var myObj = document.createElement('Object');
myObj.type = 'video/mpeg'; // No loop
myObj.type = 'text/html'; // Loop
myObj.data = 'http://google.com';
bd.appendChild(myObj);


Thank you Gérard.
GTalbot
Registered Member
Posts
69
Karma
1
OS

 
Reply to topic

Bookmarks



Who is online

Registered users: Baidu [Spider], bcooksley, Bing [Bot], brand, capslock, djarvie, Exabot [Bot], Google [Bot], GreatEmerald, jensreuterberg, jgrulich, jitseniesen, korakios, koriun, Kver, lazyit, lueck, Majestic-12 [Bot], MiceAreVeryNice, NickElliott, TheraHedwig, Tuukka, Yahoo [Bot]