Twitter的userscript的影响要大于预期(@included)之外的其他网页? [英] Twitter userscript affects pages other than the intended (@included) ones?

查看:623
本文介绍了Twitter的userscript的影响要大于预期(@included)之外的其他网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面UserScript是为了工作,我自己的Twitter个人资料页(而非时间轴)。

The below UserScript is meant to work on my own twitter profile page (not the timeline).

// ==UserScript==
// @name            CoolScript
// @include         https://twitter.com/IJNanayakkara
// @include         https://twitter.com/IJNanayakkara/status/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require         https://gist.github.com/raw/2625891/waitForKeyElements.js
// @version         1.0
// @license         GPL v3 or any later version (http://www.gnu.org/copyleft/gpl.html)
// @grant           GM_addStyle
// ==/UserScript==

function prependToTweet(jNode) {
    jNode.prepend("Hello World ");
}

waitForKeyElements("p.js-tweet-text", prependToTweet);

它的作用是追加给定文本字符串在页面的每鸣叫的开始。

What it does is it appends a given text string to the beginning of every tweet in the page.

虽然剧本做什么,我想,我面临两个问题。

Although the script does what I want to, I'm facing two issues.

1。脚本影响比 @include 中定义的其他页面。

1. Script affects pages other than the ones defined in @include.

我安装脚本,去我的Twitter个人资料。和所有的鸣叫具有的的Hello World 的字符串添加到开始。这是预期。

I install the script and go to my twitter profile. And all the tweets have the Hello World string added to the beginning. This is expected.

但后来我去我的时间线页面,所有的鸣叫那边也有的Hello World 的字符串追加到他们这是不是一种预期的行为。

But then I go to my timeline page and all the tweets over there also have the Hello World string appended to them which is not an intended behavior.

我要知道为什么会这样。

I need to know why this is happening.

2。 的Hello World 的串串重复自己。

2. Hello World string string duplicates itself.

说我点击鸣叫,以查看它在一个单一的页面。正常工作。

Say I click on a tweet to view it in a single page. Works fine.

然后,我回到我的个人资料页。

Then I go back to my profile page.

如果我去其他一些网页,并返回再次,该字符串已经重复一次​​。

If I go some other page and return again, the string has repeated once more.

这些都是我所面临的两个问题。我想这些的发生是由于AJAX,但我不能想出一个办法来解决它。

These are the two issues I'm facing. I assume these occur due to AJAX but I can't figure out a way to tackle it.

推荐答案

有几件事情似乎怎么回事:

A few things seem to be going on:

  1. 在微博变成一个新的页面,它有时会保留修改后的文本,但象垃圾一样清除jQuery的数据 waitForKeyElements 用于跟踪节点。

当导航到新,无用的网页,因为没有页面加载,脚本不重烧和老 waitForKeyElements 仍然活跃

When navigating to "new", "unwanted" pages, since there is no page load, the script does not refire and the old waitForKeyElements is still active.

如果你开始了一个页面中,你不想要的调整,然后导航到一个页面中,你这样做,那么脚本不火,因为微博只显示一个新的URL,它没有实际加载太多东西。

If you start out on a page were you don't want the adjustments and then navigate to a page were you do, then the script doesn't fire because twitter just displayed a new URL, it didn't actually load much of anything.

Twitter正在加载内置页框和脚本是在射击这些呢。

Twitter is loading iframes and the script is firing on those too.


要解决这些问题,我们:


To work around the problems we:

  1. 在同一个硬盘属性跟踪变化的节点 - 这叽叽喳喳不垃圾箱
  2. 更改 @include 来开火 https://twitter.com/* 然后用 location.pathname 来进一步细化。
  3. 停用 waitForKeyElements AJAXing不需要的网页的时候。
  4. 显示器<冠军> 的变化作为一个可靠的方法来检测新的页面
  5. 使用 @noframes
  1. Also track changed nodes by a hard attribute -- which twitter does not trash.
  2. Change the @include to fire on https://twitter.com/* and then use location.pathname to refine that further.
  3. Deactivate waitForKeyElements when AJAXing to unwanted pages.
  4. Monitor <title> changes as a reliable way to detect "new" pages.
  5. Use @noframes.

所以,这个脚本应该更好地为您(只考Greasemonkey的,但是的应该的上Tampermonkey工作):

So, this script ought to work better for you (only tested on Greasemonkey, but should work on Tampermonkey):

// ==UserScript==
// @name            Twitter: Modify tweets on select pages.
// @include         https://twitter.com/*
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require         https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant           GM_addStyle
// @noframes
// ==/UserScript==
var tweetSelector = "p.js-tweet-text";

function fireMainCode () {
    /*--- Only fire on desired pages. This fires on:
        twitter.com/IJNanayakkara*
        twitter.com/IJNanayakkara/status/*
    */
    if (/^\/IJNanayakkara(\/status\/)?/.test (location.pathname) ) {
        if ( ! this.WFKE_fired) {
            waitForKeyElements (tweetSelector, prependToTweet);
            this.WFKE_fired = true;
        }
    }
    else {
        //--- Undesired page, shut off waitForKeyElements
        if (this.WFKE_fired) {
            this.WFKE_fired = null;
            var controlObj      = waitForKeyElements.controlObj  ||  {};
            var controlKey      = tweetSelector.replace (/[^\w]/g, "_");
            var timeControl     = controlObj [controlKey];
            if (timeControl) {
                clearInterval (timeControl);
                delete controlObj [controlKey]
            }
        }
    }
}
fireMainCode ();

function prependToTweet (jNode) {
    var altDoneFlag = jNode.attr ("data-altdoneflag");
    if ( ! altDoneFlag) {
        jNode.prepend ("XXXXX => ")
        .attr ("data-altdoneflag", "yes");
    }
}

/*--- Twitter uses some pretty screwy AJAX to "change" pages, but it at least
    changes the title. So listen for that to (re) trigger waitForKeyElements.
*/
var myObserver  = new MutationObserver (titleChangeDetector);
var obsConfig   = {
    //-- Subtree needed.
    childList: true, characterData: true, subtree: true
};

myObserver.observe (document, obsConfig);

function titleChangeDetector (mutationRecords) {
    mutationRecords.forEach ( function (mutation) {
        //-- Sensible, Firefox
        if (    mutation.type                       == "childList"
            &&  mutation.target.nodeName            == "TITLE"
        ) {
            fireMainCode ();
        }
        //-- WTF, Chrome
        else if (mutation.type                      == "characterData"
            &&  mutation.target.parentNode.nodeName == "TITLE"
        ) {
            fireMainCode ();
        }
    } );
}

这篇关于Twitter的userscript的影响要大于预期(@included)之外的其他网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆