我该如何组合这两个Greasemonkey脚本
How do I combine these 2 Greasemonkey scripts?
我需要为GM组合这两个脚本。一个从列表中打开新页面,另一个单击"关注"按钮。
脚本1:如何自动按顺序打开页面列表?
脚本2:如何用Greasemonkey点击这个按钮?
我试着自己将它们组合在一起,但未能创建一个完全重新加载页面的工作脚本,即使它们是按顺序放在列表中的(如果你读了另一个问题,你就会明白我的意思)。
这是我尝试过的,但它没有像预期的那样工作,因为它没有正确地重新加载页面并继续执行任务:
// ==UserScript==
// @name Follow People on INK361
// @description Follow People from our FB Page's list INK361
// @include http://ink361.com*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design
change introduced in GM 1.0.
It restores the sandbox.
*/
var urlsToLoad = [
'http://ink361.com/#/users/30742610/photos',
'http://ink361.com/#/users/193869245/photos',
'http://ink361.com/#/users/215062853/photos',
'http://ink361.com/#/users/218295575/photos'
];
/*--- Since many of these sites load large pictures, Chrome's and
Firefox's injection may fire a good deal before the image(s)
finish loading.
So, insure script fires after load:
*/
//--- Catch new pages loaded by WELL BEHAVED ajax.
window.addEventListener ("hashchange", FireTimerA, false);
function FiretimerA () {
waitForKeyElements ("a.simplebutton:contains('follow')", FireTimer());
}
function FireTimer (jNode) {
if ( ! /^'s*follow's*$/i.test () ) {
return false;
}
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
GotoNextURL();
}
function GotoNextURL () {
var numUrls = urlsToLoad.length;
var urlIdx = urlsToLoad.indexOf (location.href);
urlIdx++;
if (urlIdx >= numUrls)
urlIdx = 0;
location.href = urlsToLoad[urlIdx];
}
合并这些脚本应该很简单:只需标准化元数据块并将一个脚本的javascript粘贴到另一个脚本
在确定合并脚本的真正目的后,我们得到:
// ==UserScript==
// @name _Follow People on INK361
// @description Follow People from our FB Page's list INK361
// @include http://ink361.com/#/users/*
// @exclude http://ink361.com/#/users/223888036*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design
change introduced in GM 1.0.
It restores the sandbox.
*/
var urlsToLoad = [
'http://ink361.com/#/users/14565576/photos'
, 'http://ink361.com/#/users/218217815/photos'
, 'http://ink361.com/#/users/202670894/photos'
, 'http://ink361.com/#/users/201771644/photos'
, 'http://ink361.com/#/users/217243779/photos'
, 'http://ink361.com/#/users/218295748/photos'
, 'http://ink361.com/#/users/218273533/photos'
, 'http://ink361.com/#/users/30742610/photos'
, 'http://ink361.com/#/users/193869245/photos'
, 'http://ink361.com/#/users/215062853/photos'
];
/*--- Operation:
1) If the button is "follow" then it clicks it.
2) If the button is, or becomes "unfollow", then go to the next page.
3) If there is no button or the button stops working, it stays on the current page.
*/
//--- Note that contains() is CASE-SENSITIVE.
waitForKeyElements (
"#relationship a.simplebutton:contains('follow')",
clickOnFollowButton
);
function clickOnFollowButton (jNode) {
if ( /^'s*follow's*$/i.test (jNode.text() ) ) {
//--- Button is "follow"; click it.
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
else if ( /^'s*unfollow's*$/i.test (jNode.text() ) ) {
//--- Unfollow button is already (or now) set. Go to next page.
jNode.text ("palate cleanser");
GotoNextURL ();
}
return true; //--- This node is reused, never mark it as found.
}
function GotoNextURL () {
var numUrls = urlsToLoad.length;
var urlIdx = urlsToLoad.indexOf (location.href);
urlIdx++;
//-- Don't loop the list of sites.
if (urlIdx < numUrls) {
location.assign (urlsToLoad[urlIdx]);
}
}
但这是假设点击"关注"按钮会使屏幕保持在同一页面上是吗(是)
我无法登录到那个网站,所以我无法完全测试脚本。如果它不起作用,请列出控制台(Firebug或Firefox)显示的任何错误消息,并准确描述它的行为。
相关文章:
- 在同一页上的两个脚本中使用全局变量
- JavaScript - 需要帮助组合两个脚本
- 加载两个脚本块(使用lab.js作为加载管理器)
- 如何使用jquery或vanillajavascript从iframe中获取两个脚本src
- 当我在一个html页面上添加两个脚本时,其中一个不起作用
- 在 HTML 上运行两个脚本(Java 脚本)
- 原型:这两个脚本之间的区别
- 两个脚本不会同时工作
- 组合两个脚本以在所有浏览器上保存Google图表时出现问题
- 如何合并两个脚本并同时触发它们
- jQuery结合了两个脚本来实现对锚点的平滑滚动
- 如何链接两个脚本
- 两个脚本出现Jade渲染错误,未捕获语法错误:意外的令牌<
- 单击来自同一元素上两个脚本的事件
- 使用jquery.on()方法处理同一元素(选项标记)的两个脚本之间的冲突
- 同一页面上的两个表单和两个脚本,第一个脚本将被调用而不考虑提交
- 使用服务器从两个脚本发送的事件
- 为什么'我的两个脚本都不能在html中工作
- 两个脚本(FitVids.js,Vimeo.js)一起杀死调用一个函数来播放视频,导致:VimeoAPI不是一个函数
- 是否可以让HTML中的一个链接执行两个脚本