Chrome扩展程序在doc start上运行 [英] Chrome extension run at doc start

查看:84
本文介绍了Chrome扩展程序在doc start上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我想将其注入文档之前加载。即;它应该像下面这样运行:

 < script ..> MYSCRIPT HERE< / script> 

< html>


我在chrome中创建了一个正确执行的脚本(我可以提醒(0)等)它运行在不同于我想要的环境中。我希望它可以在SAME环境中以页面的形式运行。



之前,我在使用启动脚本的地方使用了一个技巧

  window.location =javascript:<我的脚本在这里> 

有效地改变了执行环境,但是在过去的几天里它不起作用。我认为它是一个铬错误修复。有没有其他的解决方法呢?注意:由于document.body等不可用,我无法将脚本标记动态添加到启动脚本中的页面。



我想向您提供更多详细信息为什么我需要这个和我以前的解决方案。
我无法控制的HTML页面:

 

< script>
函数a(){// DOSOMETHING}
< / script>


< script>
a(); << ------我不想打电话给这
< / script>

珍贵的解决方案:
启动javascript包含

  window.location ='javascript:const a = function(){};';<<< -CONST used !! 

^^这会强制页面实际加载时重新声明'a'的错误,因此,当调用a()时,什么都不会发生。 (聪明,对吗?-_-)



但是现在,我意识到当我做window.location =js:..时,即使是在单独的env中运行!

帮助! :)

解决方案

Chrome在当前版本中可能有一个bug,正如PAEz指出的那样。但是,是的,很多关于如何使用chrome扩展插入文档的知识。感谢迈克的职位。下次我使用greesemonkey或其他东西进行注入时,请记住这一点:)


I have a script that I want to be injected into the document BEFORE load. I.E; it should function as if

<script..>MYSCRIPT HERE</script>

<html>
.
.

I have made a script in chrome that executes correctly (I can alert(0) etc) but, it runs in different environment which is not what I want. I want it to run in the SAME environment as page.

Earlier, I had used a trick where in the startup script I had used

window.location="javascript:<MY SCRIPT HERE>"

which effectively changes the execution environment but for the past few days it isn't working. I think its a chrome bug fix. Is there any other workaround for this? Note: I can't add script tags dynamically to the page in the startup script because the document.body etc are unavailable.

I would like to provide you more details of why I need this and my previous solution. HTML Page which I don't have control over:

.
.
<script>
function a(){//DOSOMETHING}
</script>
.
.
<script>
a(); <<------ I DONT WANT TO CALL THIS
</script>

Precious solution: Startup javascript contains

window.location='javascript:const a=function(){};';<<-CONST used!!

^^That will force error of re-declaration of 'a' when the page actually loads, hence, when a() is called, nothing happens. (clever, right? -_-)

But now, I realized when I do window.location="js:..", even that runs in separate env!

Help! :)

解决方案

Chrome probably has a bug in the current build as PAEz pointed out. But yes, a lot of insight on how things can be injected into "document" using chrome extension. Thanks Mike for the post. Will keep that in mind the next time I'm injecting using greesemonkey or something :)

这篇关于Chrome扩展程序在doc start上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆