简体   繁体   English

使用Chrome扩展程序替换jQuery脚本

[英]Replacing a jQuery script using a Chrome Extension

A website is running a jQuery script. 网站正在运行jQuery脚本。 I want to use a Chrome Extension to have the site run my own version of the jQuery script, and not the normal one. 我想使用Chrome扩展程序让网站运行我自己的jQuery脚本版本,而不是正常版本。

So far, I've managed to make the chrome extension find where the website calls the normal script, and I've replaced it with my own: 到目前为止,我设法使chrome扩展程序可以找到网站调用普通脚本的位置,并用自己的脚本替换了它:

document.querySelector("script[src^='website.com/originaljqueryscript']").src = 'myjqueryscript';

As a test, I made myjqueryscript the exact same script as the originaljqueryscript. 作为测试,我使myjqueryscript与原始jqueryscript完全相同。 I set the run_at in the manifest to run at document_end. 我将清单中的run_at设置为在document_end处运行。

When I try to open the website with my script enabled, the console logged an error $(...).dialog is not a function . 当我尝试在启用脚本的情况下打开网站时,控制台记录了错误$(...).dialog is not a function I think this is because jQuery is not loaded in my chrome extension. 我认为这是因为jQuery扩展程序未加载jQuery。 So then I found which version of jQuery the website is using, and added that to my chrome extension. 因此,我找到了网站正在使用的jQuery版本,并将其添加到了chrome扩展程序中。 Now I get this error: $(…).dialog is not a function I believe that error is due to a conflict between the two jQuerys that have been loaded (one on the website, one from my extension). 现在,我收到此错误: $(…).dialog is not a function我认为该错误是由于已加载的两个jQuery(网站上的一个,扩展名中的一个)之间的冲突引起的。

Am I on the right track, or is there a better way to replace a websites jQuery script with my own? 我是在正确的轨道上吗,还是有更好的方法用我自己的网站替换jQuery脚本?

If this is for a very specific website, loading jQuery from a specific URL, you can use webRequest API to intercept the request to jQuery and redirect it to a file bundled in your extension. 如果这是针对非常特定的网站(从特定的URL加载jQuery),则可以使用webRequest API拦截对jQuery的请求,并将其重定向到扩展中捆绑的文件中。 Eg: 例如:

chrome.webRequest.onBeforeRequest.addListener(
  function(details) { return {redirectUrl: chrome.runtime.getUrl("js/myjquery.js")}; },
  {urls: ["https://example.com/js/jquery.js"]},
  ["blocking"]
);

(Note: this sample is very minimal; you may need to include and inspect request headers to make sure that the source page is your target site - you really don't want to replace a CDN-provided jQuery for all sites) (注意:该示例非常少;您可能需要包括并检查请求标头,以确保源页面是您的目标站点-您确实不想为所有站点替换CDN提供的jQuery)

This assumes that the website does not use Subresource Integrity checks, however I believe that it will bypass a script-src Content Security Policy (redirect is transparent). 这假设该网站未使用子资源完整性检查,但是我相信它将绕过script-src内容安全策略(重定向是透明的)。


Note that .dialog() is part of jQuery UI, not core jQuery; 注意, .dialog()是jQuery UI的一部分,而不是核心jQuery。 the site presumably loads both, and you'll need to intercept both. 该网站可能会同时加载这两者,因此您需要拦截两者。 It's possible that the site actually bundles them together. 该网站实际上可能将它们捆绑在一起。

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

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