简体   繁体   English

从Chrome扩展程序读取页面的链接脚本

[英]Read page's linked script from chrome extension

I am building a Chrome Extension with the ability to detect custom javascript code from a Browser Action. 我正在构建一个能够从浏览器操作中检测自定义JavaScript代码的Chrome扩展程序。

So far, I managed to detect when the custom code is in the page itself, but I can't detect it when it is loaded from an external .js file (and that's the purpose of this question, I want to detect the content of a <script src="http://www.externaldomain.com/file.js"></script> 到目前为止,我设法检测到自定义代码何时在页面本身中,但是当它从外部.js文件加载时却无法检测到(这就是这个问题的目的,我想检测其中的内容<script src="http://www.externaldomain.com/file.js"></script>

I know this could be achieved since Google did it in its "Tag Assistant" extension but checking their source code, everything is minified and it's hard to figure out how they did it. 我知道这是可以实现的,因为Google在其“ Tag Assistant”扩展程序中做到了,但是检查其源代码后,一切都变得很小了,很难弄清楚他们是如何做到的。 eg. 例如。 Tag Assistant 标签助手

Do you have any idea of which API/trick they used ? 您是否知道他们使用了哪个API /技巧?

When a website loads up, it runs all the scripts kind of like the console on chrome. 网站加载后,它会运行所有脚本,就像chrome上的控制台一样。 if you run alert("hello") using the console,it will run once, and you will have no way to get it back using javascript. 如果您使用控制台运行alert("hello") ,它将运行一次,并且您将无法使用javascript将其恢复。 Similarily, embedded js files are all run once, and you can't just look into the html or anywhere else to get the code back since the js file is unloaded after being run. 类似地,嵌入式js文件全部运行一次,并且您不能仅仅查看html或其他任何地方来获取代码,因为js文件在运行后便被卸载了。 All the functions are stored in variables, events linked to those functions, etcetera. 所有功能都存储在变量,链接到这些功能的事件等中。

What you can do, and probably will have to do is re-fetch those files using ajax. 您可以做,并且可能要做的是使用Ajax重新获取这些文件。

I think that is exactly what tag assistant does. 我认为这正是标签助手所做的。 After looking for a while I found this: 寻找了一段时间后,我发现了这一点:

//for each document.script, get the src, and load it with ajax using the function gf(src,return function), and send the response.responseText to the text identification functions
cf.prototype.qa = function(a, b) {
    var c = {};
    Ea(document.scripts, function(d) {//for each
        !d.src || "" == d.src || c[d.src] || ef(this, d.src) || d.src == a || (c[d.src] = !0, gf(d.src, function(a) {
            cf.M().W.forEach(function(c) {
                c.u() && (c = c.qa(a.responseText, d.src), c.length && b(c, d.src))
            })
        }))
    }, this)
};

function gf is a simple ajax call with a callback function gf函数是带有回调函数的简单ajax调用

gf = function(a, b) {
    try {
        var c = new XMLHttpRequest;
        c.open("GET", a, !0);
        var d = !1;
        c.onreadystatechange = function() {
            d || 4 != c.readyState || 200 != c.status || (d = !0, b(c))
        };
        c.send()
    } catch (e) {}
}

source from chrome-extension://kejbdjndbnbjgmefkgdddjlbokphdefk/tag_assistant.js (the content script) 源自chrome-extension://kejbdjndbnbjgmefkgdddjlbokphdefk/tag_assistant.js (内容脚本)

Seeing what tag assistant does, I suggest you get every script element, retrieve its src,and in your content scripts, run a simple ajax call like the one on this page: http://code.tutsplus.com/articles/how-to-make-ajax-requests-with-raw-javascript--net-4855 查看标签助手的功能后,建议您获取每个脚本元素,检索其src,然后在内容脚本中运行一个简单的ajax调用,例如此页面上的调用: http : //code.tutsplus.com/articles/how-对做的Ajax-请求与-原始的JavaScript -网4855

//for each script, get its src,and do whatever you want with it
for(i=0;i<document.scripts.length;i++){
    load(document.scripts[i].src, function(xhr){
       myCodeAnalyzingFunction(xhr.responseText)
    })
}

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

相关问题 是否允许Chrome扩展程序通过内容脚本读取页面脚本内容 - Is it allowed for a Chrome Extension to read page script content via Content Script 如何读取当前页面的HTML以访问Chrome扩展程序中的元素? - How to read current page's HTML to access an element from a Chrome extension? 如何从Chrome扩展程序的选项页面启用/禁用manifest.json CSS内容脚本? - How to enable/disable manifest.json CSS content script from Chrome Extension's options page? Chrome扩展程序:从content_script访问页面的HTML5 localStorage - Chrome Extension: access page's HTML5 localStorage from content_script 与Chrome扩展程序中内容脚本中的页面脚本进行通信 - Communicate with a page script from a content script in a Chrome extension 用于修改页面脚本的 Chrome 扩展包括和 JS - Chrome extension to modify page's script includes and JS 如何从 cjs chrome 扩展读取 js 脚本文件 - How to read a js script file from cjs chrome extension Chrome扩展程序 - 将对象从页面传递到上下文脚本 - Chrome Extension - Passing object from page to context script 从chrome扩展内容脚本执行网页js - Execute web page js from chrome extension content script 将选项页面中的设置交换为Chrome扩展程序中的内容脚本 - Exchanging Settings from Options Page to Content-Script in Chrome Extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM