简体   繁体   English

如何使用小书签加载javascript文件,而小书签又不会重定向/重写页面?

[英]How can i load a javascript file using a bookmarklet that in turn does not redirect/rewrite the page?

I'm trying to load this remote debugging solution to debug/interact with DOM/js live. 我正在尝试加载此远程调试解决方案以与DOM / js live进行调试/交互。 The instructions ask you to put a script block and script link into the HEAD of the page to get it all working. 说明要求您将script块和script链接放入页面的HEAD中,以使其全部正常工作。 That's fine. 没关系。 It's a great tool. 这是一个很棒的工具。

But what if i wanted a bookmarklet-like way of just "inserting" it into any page. 但是,如果我想要一种类似于bookmarklet的方式将其“插入”到任何页面中,该怎么办? I don't see why this can't be done. 我不明白为什么不能做到这一点。

This is my bookmarklet: 这是我的书签:

javascript:(function(){document.body.appendChild(document.createElement('script')).src='http://url-to-js-file.js?x='+(Math.random());})();

And then "url-to-js-file.js" loads the js for iphonedebug : 然后“ url-to-js- file.js ”为iphonedebug加载js:

ipdConfig = {
    baseUrl: '/ipd/', // trailing slash!
    consoleBaseUrl: 'http://192.168.1.25:8170/' // trailing slash!
}

document.body.appendChild(document.createElement('script')).src='http://192.168.1.25/ipd/ipd.js';

console.log('fin.');

Note that the src property there originally was "/ipd/ipd.js" like the instructions suggest. 请注意,最初的src属性为“ /ipd/ipd.js”,如说明所示。 But it will need to be absolute like this if i want to get the debugger working with other sites. 但是,如果我想让调试器与其他站点一起工作,就必须像这样绝对。

My problem is that this code will dump the first js file (url-to-js-file.js) into the page ok but when the first file tries to insert the other successive files my page appears to redirect. 我的问题是此代码会将第一个js文件(url-to-js-file.js)转储到页面ok中,但是当第一个文件尝试插入其他后续文件时,我的页面似乎会重定向。 In fact it does not redirect but just get rewritten. 实际上,它不会重定向,而只会被重写。 Firebug shows the resulting page as no body but just the ipd js files in the HEAD element. Firebug将结果页面显示为无内容,而仅显示HEAD元素中的ipd js文件。

Why does it do this? 为什么这样做呢?

Is it because of the bookmarklet behavior that normally redirects to a new page? 是否由于通常会重定向到新页面的书签行为 I don't think so. 我不这么认为。 Because i'm using a closure in the bookmarklet link. 因为我在小书签链接中使用了闭包。 And when i comment out the document.body... line of the "url-to-js-file.js" file the page stays where it is. 当我注释掉“ url-to-js-file.js”文件的document.body...行时,页面保持原样。 Is it the nesting? 是嵌套吗? Or may it be something in the other ipd js files causing this? 还是可能是其他ipd js文件中的某些内容导致了此问题?

I've tried putting my "url-to-js-file.js" code into a closure but then the global var ipdConfig becomes invisible to the rest of the ipd code which breaks it. 我尝试将我的“ url-to-js-file.js”代码放到闭包中,但随后的全局ipdConfig对其余的ipd代码变得不可见,从而破坏了它。

Any ideas? 有任何想法吗?

My best guess is that the ipd.js file is using document.write() somewhere in its code. 我最好的猜测是ipd.js文件在其代码中的某处使用document.write() In which case you need to modify ipd.js so that it doesn't do this. 在这种情况下,您需要修改ipd.js使其不执行此操作。

As I've suspected, ipd.js is written by a javascript newbie: 我怀疑ipd.js是由javascript新手编写的:

document.write('<script src="' + ipdConfig.baseUrl + '_ipd.js"></script>');

Modify it to use appendChild(createElement(...)) instead. 修改它以使用appendChild(createElement(...))代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM