使用chrome扩展程序将运行JavaScript函数的链接注入网站

时间:2015-12-29 08:53:59

标签: javascript html google-chrome google-chrome-extension code-injection

我想在此网页的末尾注入一个链接。

Website

该链接将从一个记录循环到另一个记录,在链接末尾添加+1,因此当点击下一个链接时,该链接将从http://lcsyndicate.com.lb/web/popupGuide.aspx?Guide_Id=5425更改为http://lcsyndicate.com.lb/web/popupGuide.aspx?Guide_Id=5426等。

为此,我创建了一个带有iframe的HTML页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title><link href="includes/ls.css" rel="stylesheet" type="text/css" /></head>
<body bgcolor="#373536" >
<iframe 
 src="http://lcsyndicate.com.lb/web/popupGuide.aspx?Guide_Id=5425"
 width="100%" height="500"
 sandbox>
  <p>
    <a href="http://lcsyndicate.com.lb/web/popupGuide.aspx?Guide_Id=5425">
      Fallback link for browsers that, unlikely, don't support frames
    </a>
  </p>


</body>
</html>

加载后的网页运行正常。但是,我不想使用iframe。所以我想创建一个Chrome扩展来注入代码。

我用它作为manifest.json:

{
  "name": "Test",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "This extension was created with the awesome extensionizr.com",
  "homepage_url": "http://extensionizr.com",
  "icons": {
    "16": "icons/icon16.png",
    "48": "icons/icon48.png",
    "128": "icons/icon128.png"
  },
  "default_locale": "en",
  "permissions": [
    "http://lcsyndicate.com.lb/web/popupGuide.aspx?Guide_Id*"
  ],
  "content_scripts": [
    {
      "matches": [
        "http://lcsyndicate.com.lb/web/popupGuide.aspx?Guide_Id*"
      ],
      "css": [
        "src/inject/inject.css"
      ]
    },
    {
      "matches": [
        "http://lcsyndicate.com.lb/web/popupGuide.aspx?Guide_Id*"
      ],
      "js": [
        "src/inject/inject.js"
      ]
    }
  ]
}

我的inject.js代码:

chrome.extension.sendMessage({}, function(response) {
    var readyStateCheckInterval = setInterval(function() {
    if (document.readyState === "complete") {
        clearInterval(readyStateCheckInterval);

        // ----------------------------------------------------------
        // This part of the script triggers when page is done loading
        console.log("Hello. This message was sent from scripts/inject.js");
        // ----------------------------------------------------------

    }
    }, 10);
});

var c=5425;
var url="http://lcsyndicate.com.lb/web/popupGuide.aspx?Guide_Id=";

function f(){ 
 document.getElementById("clicks").data = url+c;
 c++;
 }

var newlink = document.createElement('a');
newlink.innerHTML = 'Next>';
newlink.setAttribute('id','clicks');
newlink.setAttribute('title', 'Next>');
newlink.onclick = f;
newlink.setAttribute('onclick',"f()");
document.body.appendChild(newlink); 

然而,当扩展程序运行时,我在页面上收到“未捕获的ReferenceError:f未定义”错误

我不知道如何修复它。我只是想添加一个从一个记录循环到另一个记录的链接。

感谢任何帮助

0 个答案:

没有答案