尝试从网页

时间:2018-04-20 21:08:15

标签: google-chrome google-apps-script google-chrome-extension google-chrome-devtools

在这里,我已经获得所有需要的许可,如果遗漏了这些内容,请告诉我。

的manifest.json

  {

  "manifest_version": 2,

  "name": "SearchMood",
  "description": "This extension shows Google Web Search and Google Image 
   Search result.",
  "version": "1.4",
  "author":"Searchmood",

  "browser_action": {
    "default_icon": {
      "128": "icon-128.png",
      "16": "icon-16.png",
      "48": "icon-48.png"
    },
    "default_title": "SearchMood",
    "default_popup": "background.html"
  },

  "chrome_url_overrides" : {
    "newtab": "popup.html"
  },

  "icons": {
    "128": "icon-128.png",
    "16": "icon-16.png",
    "48": "icon-48.png"
  },
  "background": {
    "scripts": [ "background.js","popup.js" ]
    },
   "permissions": [
    "activeTab","management","https://ajax.googleapis.com/"
  ],

  "content_scripts": [
    {
      "matches": [ "http://search.searchmood.com/*" ],
      "js": [ "js/restoremodal.js" ],
      "all_frames": true,
      "run_at": "document_start"
    }
],
  "externally_connectable": {
  "matches": ["http://*.searchmood.com/*"]
}
}

我的网页上的脚本我有:

     <div class="links">

                        <ul>

                         <li><a href="terms.php">Terms&Conditions</a></li>&nbsp;|&nbsp;

                         <li><a href="privacy.php">Privacy policy</a></li>|&nbsp;

                         <li><a href="Contact.php">Contact US</a></li>
             <li><span id="restoreLink" onclick="adi();">Reset Chrome</span></li>
                        </ul>

                      </div>


        <script type="text/javascript">

function adi(){
      var editorExtensionId = "pokioadkjpcbalcpfidmlebofahebkhb";
    // Make a simple request:
    chrome.runtime.sendMessage(editorExtensionId, {request: "uninstall"});
}
     </script>

这是我的扩展中的代码来收听此消息我将此代码放在popup.js中。请指导我把它放在哪里。

 chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    if (request == "uninstall") {
       var id = chrome.app.getDetails().id;
       chrome.management.setEnabled(id, false); 
    }
  });

这是我写的脚本。我还有一些其他代码,但这也无效。

1 个答案:

答案 0 :(得分:0)

是的我得到了答案在内容脚本文件中我们可以编写从网站获取元素的代码他们将使用SendMessage函数确保您已获得&#34;管理&#34;在manifest.json中并且在Menifest.json中持久化应该是&#34; false&#34;。

然后您可以使用onmessage功能并禁用扩展代码供您参考:

**HTML**`<span id="restorelink"> disable chrome extension</span>`

**menifest.json**

    {
  "manifest_version": 2,

  "name": "name",
  "description": "describe about you extension",
  "version": "1",

  "browser_action": {
    "default_icon": {
      "128": "icon-128.png",
      "16": "icon-16.png",
      "48": "icon-48.png"
    },
  "background": {
    "scripts": [ "background.js"],
    "persistent": false
    },
   "permissions": [
    "management"
  ],

  "content_scripts": [
    {
      "matches": [ "http://your site url where you want to apply the change, you can use more than one seperating with "," ],
      "js": [ "restoremodal.js" ],
      "all_frames": true
    }
]
}

**restoremodal.js**

     document.getElementById("restoreLink").addEventListener("click", adi);
 function adi(){
 chrome.runtime.sendMessage({ value: "anything"});
 }


**Background.js**
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.value=="anything") {
    var id = chrome.app.getDetails().id;
    chrome.management.setEnabled(id, false); 
    }

  });