油猴脚本执行页面函数

作者: zengde 分类: 技术相关 发布时间: 2018-12-27 02:17

1.使用unsafeWindow替代window

2.使用@grant none

// ==UserScript==
// @name        Content Script Example, grant none
// @grant       none
// ==/UserScript==

// Because I am a "@grant none" script, I operate right in the content scope.
// I can call functions defined by the page with no problems.
var result = functionFromContentScope();

// Similarly, I can export values or functions to that scope for it to call.
window.someVariable = 'someValue';

3.使用了@grant后,使用window.eval

// ==UserScript==
// @name        Content Script Example, privileged grant
// @grant       GM_log
// ==/UserScript==

// Because I am *not* a "@grant none" script, I operate in my own private,
// privileged scope.  I can call functions defined by the page only by
// explicitly referencing the window:
var result = window.eval('functionFromContentScope();')

// Similarly, I can export values or functions to that scope for it to call.
window.eval("window.someVariable = 'someValue';");

参考:

https://wiki.greasespot.net/Category:Coding_Tips:Interacting_With_The_Page

https://wiki.greasespot.net/Generate_Click_Events