gpt4 book ai didi

javascript - Tampermonkey 脚本未运行 jquery

转载 作者:行者123 更新时间:2023-12-03 02:20:01 28 4
gpt4 key购买 nike

我正在尝试使用 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);

但这也行不通。有人可以帮我解决使用 Tamper Monkey 执行按钮单击的问题吗?

最佳答案

这是因为没有为输入元素的 css 定义 z-index 元素,因此输入按钮被 div 遮盖并使其不可点击

下面是工作代码

$(document).ready(function() {
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).css("z-index", 1000);
$('#CP').click(function(){
$.each(hrefs, function(index, value) {
setTimeout(function(){
window.open(value, '_blank');
},1000);
});
});
});

关于javascript - Tampermonkey 脚本未运行 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49200525/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com