Chrome扩展程序:如何将文本插入或粘贴到推文中

时间:2018-02-05 20:50:28

标签: google-chrome-extension insert

让我疯了..背景故事..我在Twitter上进入了很多比赛,并发现自己重复复制/粘贴个人资料链接和标记朋友的常见任务。 :)

所以我认为,扩展可能会让这更容易!

清单:

{
   "name": "Context Menus Sample (with Event Page)", 
  "description": "Shows some of the features of the Context Menus API using an event page",
  "version": "0.7",
  "permissions": ["contextMenus", "tabs", "clipboardWrite", "clipboardRead" ],
  "background": {
    "persistent": false,
    "scripts": ["sample.js"]
  },
  "manifest_version": 2
}

sample.js

function onClickHandler(info, tab) {
    if (info.menuItemId == 'h_profile') { 
        // here is where it needs to put the text   

     }

};

chrome.contextMenus.onClicked.addListener(onClickHandler);

  // Set up context menu tree at install time.
chrome.runtime.onInstalled.addListener(function() {
        chrome.contextMenus.create({"title": "HELLCASE", "contexts":["editable"], "id": "hellcase"});
    chrome.contextMenus.create({"title": "PROFILE", "contexts":["editable"], "parentId": "hellcase", "id": "h_profile"});

});

我可以让它触发,就像点击的警告...但无论我尝试什么我都不能让它在twitter上插入文本到焦点textarea(div) 我认为这可能是因为它只是一个div而不是像textarea这样的实际元素...... 看起来应该很容易,但它不会工作!洛尔

任何想法?

更新:似乎我需要更多的东西 - 我不知道它们是否全部都需要但它现在有用了!

{
  "name": "TwitterHelper",
  "description": "Helps to make entering contests easier!",
  "version": "0.5",
  "permissions": [
    "contextMenus","tabs","activeTab"],
  "background": {
    "persistent": false,
    "scripts": ["myscript.js"]
  },
  "browser_action": {
    "default_icon": "icon.png"
  },

  "manifest_version": 2
}

myscript.js

function onClickHandler(info, tab) {
    if (info.menuItemId == 'h_profile') { 
            SendText('profile');
     }

};

function SendText(strText){
            chrome.tabs.executeScript({
            code: 'var temp = document.activeElement.innerHTML; document.activeElement.innerHTML=temp + "' + strText + '";'
          });
}

chrome.contextMenus.onClicked.addListener(onClickHandler);

  // Set up context menu tree at install time.
chrome.runtime.onInstalled.addListener(function() {
        chrome.contextMenus.create({"title": "HELLCASE", "contexts":["editable"], "id": "hellcase"});
    chrome.contextMenus.create({"title": "PROFILE", "contexts":["editable"], "parentId": "hellcase", "id": "h_profile"});

});

0 个答案:

没有答案