如何在页面执行之前读取/拦截JavaScript?

时间:2016-05-22 19:40:00

标签: firefox-addon

我想通过Firefox API拦截#include <stdio.h> #include <stdlib.h> #include <malloc.h> struct AVLTree { int data; struct AVLTree *left; struct AVLTree *right; }; struct AVL *RightRotate1(struct AVLTree *rootop,struct AVLTree *root) { if(root == NULL) { return NULL; } if(rootop->data > root->data) { root->right = RightRotate1(rootop,root->right); } else if(rootop->data < root->data ) { root->left = RightRotate1(rootop,root->left); } else { struct AVLTree *A = rootop->left; rootop->left = A->right; A->right = rootop; return A; } return root; } int main() { struct AVLTree * root = (struct AVLTree *)malloc(sizeof(struct AVLTree)); root-> data = 9; struct AVLTree * l = (struct AVLTree *)malloc(sizeof(struct AVLTree)); l -> data = 5; struct AVLTree * ll = (struct AVLTree *)malloc(sizeof(struct AVLTree)); ll -> data = 3; ll -> left = ll -> right = NULL; struct AVLTree * lll = (struct AVLTree *)malloc(sizeof(struct AVLTree)); lll -> data = 2; lll -> left = lll -> right = NULL; ll->left = lll; struct AVLTree * llll = (struct AVLTree *)malloc(sizeof(struct AVLTree)); llll -> data = 1; llll -> left = llll -> right = NULL; lll->left = llll; struct AVLTree * lr = (struct AVLTree *)malloc(sizeof(struct AVLTree)); lr -> data = 7; lr -> left = lr -> right = NULL; l -> left = ll; l -> right = lr; struct AVLTree * r = (struct AVLTree *)malloc(sizeof(struct AVLTree)); r -> data = 13; struct AVLTree * rl = (struct AVLTree *)malloc(sizeof(struct AVLTree)); rl -> data = 11; rl -> left = rl -> right = NULL; struct AVLTree * rr = (struct AVLTree *)malloc(sizeof(struct AVLTree)); rr -> data = 17; rr -> left = rr -> right = NULL; r -> left = rl; r -> right = rr; root -> left = l; root -> right = r; root = RightRotate1(l,root); printf("Root is %d\n",root->data); printf("Root Left is %d\n",root->left->data); printf("Root Left Left is %d\n",root->left->left->data); printf("Root Left Right is %d\n",root->left->right->data); printf("Left is %d\n",l->data); printf("Left left is %d\n",ll->data); printf("Left right is %d\n",lll->data); return 0; } ,或者在加载/执行之前阅读页面上的JS(远程和嵌入式),或者通过任何其他方式拦截。{/ p>

示例:

location.reload();

我尝试了<head> <script> window.setTimeout(function() { location.reload(); }, 10000); </script> </head> 事件监听器(通过GreaseMonkey&amp; beforescriptexecute),但是在执行上面之后它被解雇了。

更新 // @run-at document-start可以很好地处理REMOTE脚本,因为在发出请求之前会触发事件beforescriptexecute(但是在脚本src而不是脚本内容上)。如果脚本在正常脚本标记内(而不是远程标记),则根据给出的示例不同。 beforescriptexecute触发器和脚本内容可以重写,但到那时beforescriptexecute已经触发并且正在执行。

1 个答案:

答案 0 :(得分:0)

beforescriptexecute应该有效。这是一次非油脂活动:

https://developer.mozilla.org/en-US/docs/Web/Events/beforescriptexecute

你可以这样做:

document.addEventListener("beforescriptexecute", function(e) {

    src = e.target.src;
    content = e.target.text;

    if (src.search("i18n.js") > -1) {
        // Stop original script
        e.preventDefault();
        e.stopPropagation();
        window.jQuery(e.target).remove();

        var script = document.createElement('script');

        script.textContent = 'script you want';

        (document.head || document.documentElement).appendChild(script);
        script.onload = function() {
            this.parentNode.removeChild(this);
        }
      }