Chrome扩展程序:在当前标签中模拟按键

时间:2018-04-11 02:58:09

标签: javascript google-chrome plugins popup.js

尽我所能(绝对不是开发者),我正在尝试开发一个Chrome扩展程序,以便在另一台计算机上恢复Netflix会话。

当我点击扩展程序时,该标签的当前网址会被发送到另一台计算机,并且会捕获该网址并启动Netflix。

它有效!

1)但是我希望当前的TAB暂停,所以模拟发送SPACE键。

请你知道我该怎么做吗?

2)另外,当我点击插件时,我必须点击“保存书签”按钮才能实际发送链接。

请问有没有办法在我实际点击插件时发送链接:基本上是发送链接而不必点击“保存书签”?

我知道这些是超级菜鸟问题,我试图找到自己,但我真的很擅长这个!

提前谢谢!

popup.js

// This callback function is called when the content script has been 
// injected and returned its results
function onPageDetailsReceived(pageDetails)  { 
    document.getElementById('title').value = pageDetails.title; 
    document.getElementById('url').value = pageDetails.url; 
    document.getElementById('summary').innerText = pageDetails.summary; 
} 

// Global reference to the status display SPAN
var statusDisplay = null;

// POST the data to the server using XMLHttpRequest
function addBookmark() {
    // Cancel the form submit
    event.preventDefault();

    // The URL to POST our data to
    var postUrl = 'http://192.168.0.16/netflix/index.php';

    // Set up an asynchronous AJAX POST request
    var xhr = new XMLHttpRequest();
    xhr.open('POST', postUrl, true);

    // Prepare the data to be POSTed by URLEncoding each field's contents
    var title = encodeURIComponent(document.getElementById('title').value);
    var url = encodeURIComponent(document.getElementById('url').value);
    var summary = encodeURIComponent(document.getElementById('summary').value);
    var tags = encodeURIComponent(document.getElementById('tags').value);

    var params = 'title=' + title + 
                 '&url=' + url + 
                 '&summary=' + summary +
                 '&tags=' + tags;

    // Replace any instances of the URLEncoded space char with +
    params = params.replace(/%20/g, '+');

    // Set correct header for form data 
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    // Handle request state change events
    xhr.onreadystatechange = function() { 
        // If the request completed
        if (xhr.readyState == 4) {
            statusDisplay.innerHTML = '';
            if (xhr.status == 200) {
                // If it was a success, close the popup after a short delay
                statusDisplay.innerHTML = 'Saved!';
                window.setTimeout(window.close, 1000);
            } else {
                // Show what went wrong
                statusDisplay.innerHTML = 'Error saving: ' + xhr.statusText;
            }
        }
    };

    // Send the request and set status
    xhr.send(params);
    statusDisplay.innerHTML = 'Saving...';
}

// When the popup HTML has loaded
window.addEventListener('load', function(evt) {
    // Cache a reference to the status display SPAN
    statusDisplay = document.getElementById('status-display');
    // Handle the bookmark form submit event with our addBookmark function
    document.getElementById('addbookmark').addEventListener('submit', addBookmark);

    // Get the event page
    chrome.runtime.getBackgroundPage(function(eventPage) {
        // Call the getPageInfo function in the event page, passing in 
        // our onPageDetailsReceived function as the callback. This injects 
        // content.js into the current tab's HTML
        eventPage.getPageDetails(onPageDetailsReceived);
    });
});

event.js

 // This function is called onload in the popup code
 function getPageDetails(callback) { 
     // Inject the content script into the current page 
     chrome.tabs.executeScript(null, { file: 'content.js' }); 
     // Perform the callback when a message is received from the content script
     chrome.runtime.onMessage.addListener(function(message)  { 
         // Call the callback function
         callback(message); 
     }); 
 }; 

popup.html

<body>
    <form id="addbookmark">
        <p><label for="title">Title</label><br />
        <input type="text" id="title" name="title" size="50" value="" /></p>
        <p><label for="url">Url</label><br />
        <input type="text" id="url" name="url" size="50" value="" /></p>
        <p><label for="summary">Summary</label><br />
        <textarea id="summary" name="summary" rows="6" cols="35"></textarea></p>
        <p><label for="tags">Tags</label><br />
        <input type="text" id="tags" name="tags" size="50" value="" /></p>
        <p>
            <input id="save" type="submit" value="Save Bookmark" />
            <span id="status-display"></span>
        </p>
    </form>
</body>

0 个答案:

没有答案
相关问题