【发布时间】:2018-03-09 19:06:18
【问题描述】:
我正在尝试使用 tamper monkey 执行一个脚本,该脚本添加一个图标并执行单击时打开网页中所有链接的任务,这里是脚本 https://gist.github.com/jameshibbard/3831983#file-gistfile1-js 的源代码。
启用脚本后,该图标会添加到页面上,但单击链接不起作用。
我发现这可能与jquery兼容性问题有关,并在加载jquery时尝试了以下更改
// ==UserScript==
// @name Open CodeProject Links
// @namespace http://hibbard.eu/
// @version 0.1
// @description Opens all of the links from the CodeProject newsletter in one go
// @match *://www.codeproject.com/script/Mailouts/*
// @copyright 2012+, hibbard.eu
// @require https://code.jquery.com/jquery-latest.js
// @grant GM_log
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.
// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
function main(){
$(document).ready(function() {
//var my_jquery = jQuery;
//jQuery.noConflict(true);
//var $ = my_jquery, jQuery = my_jquery;
var hrefs = new Array();
var elements = $('.headline > a');
elements.each(function() {
hrefs.push($(this).attr('href'));
});
$('body').append('<input type="button" value="Open Links" id="CP">');
$("#CP").css("position", "fixed").css("top", 0).css("left", 0);
$('#CP').click(function(){
$.each(hrefs, function(index, value) {
setTimeout(function(){
window.open(value, '_blank');
},1000);
});
});
});
}
// load jQuery and execute the main function
addJQuery(main);
但这也行不通。有人可以帮我解决使用篡改猴子执行按钮单击的问题吗?
【问题讨论】:
标签: javascript jquery tampermonkey