没有为具有主题标签(并且没有www)的URL执行内容脚本?

时间:2012-11-25 21:46:55

标签: javascript google-chrome-extension hashtag content-script

此问题似乎已得到解决,只要您注入javascript的网页的网址以www开头即可。如果没有,你会怎么做?这是我的清单的相关部分:

"content_scripts": [
  {
  "run_at": "document_start",
  "matches": ["https://groups.google.com/forum/?fromgroups=#!newtopic/opencomments-site-discussions"],
  "js": ["postMsg.js"]
  }
],

根据另一个stackoverflow帖子,这个问题是因为页面的网址并不以www'开头。这是否意味着您无法将javascript注入其URL不以www'开头的安全页面,还是有其他方式?这在过去从来就不是问题,因为我的扩展已经与版本1清单一起运行。

忘记添加内容脚本:

var subject = document.getElementById("p-s-0");

subject.setAttribute("value", "foo");   

ID为" p-s-0"是Google网上论坛帖子页面中的主题字段,因此该字段应显示" foo"。

1 个答案:

答案 0 :(得分:0)

一些问题:

  1. 这是无效的match pattern,因为它们只指定了网址路径?之前的部分)。

    matches更改为:

    "matches": ["https://groups.google.com/forum/*"],
    


  2. 整体网址(https://groups.google.com/forum/?fromgroups=#!newtopic/opencomments-site-discussions)不切实际,因为Google会毫不客气地更改网址参数。例如,fromgroups通常不存在,如果是,则可能没有=。其他参数,例如hl=en来来去去。 (这就是我之前的回答对我有用的原因,但不适合你。)

    因此,在清单中使用include_globs将是一个混乱,容易出错的练习 解决方案是检查内容脚本中的location.hash

  3. 脚本设置为"run_at": "document_start",因此内容脚本在任何标识为p-s-0的节点之前运行。

    将清单更改为"run_at": "document_end"

  4. 新的Google群组是由AJAX驱动的。因此,“新主题”页面通常是“加载”而不实际加载整个新页面。这意味着内容脚本不会重新运行。它需要监视“新的”加载AJAX的页面。

    通过监控the hashchange event来检查“新”页面。

  5. 此外,p-s-0元素由AJAX添加,并且不会立即在“新”页面上提供。在setInterval

  6. 中检查此元素

    全部放在一起,
    manifest.json 变为:

    {
        "manifest_version": 2,
        "content_scripts": [ {
            "run_at":           "document_end",
            "js":               [ "postMsg.js" ],
            "matches":          [ "https://groups.google.com/forum/*" ]
        } ],
        "description":  "Fills in subject when posting a new topic in select google groups",
        "name":         "Google groups, Topic-subject filler",
        "version":      "1"
    }
    


    内容脚本(postMsg.js)变为:

    fireOnNewTopic ();  // Initial run on cold start or full reload.
    
    window.addEventListener ("hashchange", fireOnNewTopic,  false);
    
    function fireOnNewTopic () {
        /*-- For the pages we want, location.hash will contain values
            like: "#!newtopic/{group title}"
        */
        if (location.hash) {
            var locHashParts    = location.hash.split ('/');
            if (locHashParts.length > 1  &&  locHashParts[0] == '#!newtopic') {
                var subjectStr  = '';
                switch (locHashParts[1]) {
                    case 'opencomments-site-discussions':
                        subjectStr  = 'Site discussion truth';
                        break;
                    case 'greasemonkey-users':
                        subjectStr  = 'GM wisdom';
                        break;
                    default:
                        break;
                }
    
                if (subjectStr) {
                    runPayloadCode (subjectStr);
                }
            }
        }
    }
    
    function runPayloadCode (subjectStr) {
        var targetID        = 'p-s-0'
        var failsafeCount   = 0;
        var subjectInpTimer = setInterval ( function() {
                var subject = document.getElementById (targetID);
                if (subject) {
                    clearInterval (subjectInpTimer);
                    subject.setAttribute ("value", subjectStr);
                }
                else {
                    failsafeCount++;
                    //console.log ("failsafeCount: ", failsafeCount);
                    if (failsafeCount > 300) {
                        clearInterval (subjectInpTimer);
                        alert ('Node id ' + targetID + ' not found!');
                    }
                }
            },
            200
        );
    }